home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / ld / dist / ld.c.bak < prev    next >
Encoding:
Text File  |  1990-02-26  |  138.3 KB  |  5,136 lines

  1. /* Linker `ld' for GNU
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Richard Stallman with some help from Eric Albert.
  19.    Set, indirect, and warning symbol features added by Randy Smith.  */
  20.  
  21. #include <ar.h>
  22. #include <stdio.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <sys/file.h>
  26. #ifndef sony_news
  27. #include <fcntl.h>
  28. #endif
  29.  
  30. #define TARGET_VAX              1
  31. #define TARGET_SUN2             2
  32. #define TARGET_SUN3             3
  33. #define TARGET_SUN4             4
  34. #define TARGET_ALTOS            5
  35. #define TARGET_I386             6   
  36. #define TARGET_HPUX             7
  37. #define TARGET_SONY_NEWS        8
  38.  
  39. /*
  40.  * If `TARGET_MACHINE' was not defined on the compiler command line,
  41.  * then set it equal to the host machine.
  42.  */
  43. #ifndef TARGET_MACHINE
  44. #if defined(sun) && defined(sparc)
  45. #define TARGET_MACHINE  TARGET_SUN4
  46. #endif
  47. #if defined(sun) && (defined(m68020) || defined(mc68020))
  48. #define TARGET_MACHINE  TARGET_SUN3
  49. #endif
  50. #if defined(sun) && (defined(m68010) || defined(mc68010))
  51. #define TARGET_MACHINE  TARGET_SUN2
  52. #endif
  53. #if defined(ALTOS)
  54. #define TARGET_MACHINE  TARGET_ALTOS
  55. #endif
  56. #if defined(hpux)
  57. #define TARGET_MACHINE  TARGET_HPUX
  58. #endif
  59. #if defined(is386)
  60. #define TARGET_MACHINE  TARGET_I386
  61. #endif
  62. #if defined(sony_news)
  63. #define TARGET_MACHINE  TARGET_SONY_NEWS
  64. #endif
  65. #if defined(sequent)
  66. #define TARGET_MACHINE  TARGET_SEQUENT
  67. #endif
  68. #if defined(is68k) && !defined(TARGET_MACHINE)
  69. #define TARGET_MACHINE  TARGET_68K
  70. #endif
  71. #endif
  72.  
  73. #ifndef TARGET_MACHINE
  74.     CANNOT COMPILE, NO TARGET MACHINE SPECIFIED
  75. #endif
  76.  
  77. #ifndef BIG_ENDIAN
  78. #define BIG_ENDIAN      4321
  79. #endif
  80. #ifndef LITTLE_ENDIAN
  81. #define LITTLE_ENDIAN   1234
  82. #endif
  83. #ifndef PDP_ENDIAN
  84. /* I think it is unlikely that anybody will try to cross compile
  85.  * to or from a pdp, so only big-endian and little-endian are supported. */
  86. #define PDP_ENDIAN   3412
  87. #endif
  88.  
  89. #if TARGET_MACHINE==TARGET_SUN4         || \
  90.     TARGET_MACHINE==TARGET_SUN3         || \
  91.     TARGET_MACHINE==TARGET_SUN2         || \
  92.     TARGET_MACHINE==TARGET_ALTOS        || \
  93.     TARGET_MACHINE==TARGET_HPUX         || \
  94.     TARGET_MACHINE==TARGET_SONY_NEWS    || \
  95.     TARGET_MACHINE==TARGET_68K
  96. #define TARGET_BYTE_ORDER       BIG_ENDIAN
  97. #endif
  98.  
  99. #if TARGET_MACHINE==TARGET_VAX          || \
  100.     TARGET_MACHINE==TARGET_I386
  101. #define TARGET_BYTE_ORDER       LITTLE_ENDIAN
  102. #endif
  103.  
  104. #if TARGET_MACHINE==TARGET_SUN3
  105. #define TARGET_PAGE_SIZE    0x2000
  106. #endif
  107.  
  108. #if defined(sparc)      || \
  109.     defined(m68000)     || \
  110.     defined(mc68000)    || \
  111.     defined(m68010)     || \
  112.     defined(mc68010)    || \
  113.     defined(m68020)     || \
  114.     defined(mc68020)    || \
  115.     defined(is68k)      || \
  116.     defined(hpux)       || \
  117.     defined(sony_news)
  118. #define HOST_BYTE_ORDER     BIG_ENDIAN
  119. #endif
  120.  
  121. #if defined(vax)        || \
  122.     defined(is386)      || \
  123.     defined(ds3100)     
  124. #define HOST_BYTE_ORDER     LITTLE_ENDIAN
  125. #endif
  126.  
  127. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  128. static void fix_byte_order();
  129. static void fix_exec_header_byte_order();
  130. static void fix_symbol_byte_order();
  131. static void target_to_host_reloc_byte_order();
  132. static void host_to_target_reloc_byte_order();
  133. static void fix_symbol_root_byte_order();
  134. #endif
  135.  
  136. #ifdef COFF_ENCAPSULATE
  137. #include "a.out.encap.h"
  138. #else
  139. #ifdef sprite
  140. #if TARGET_MACHINE==TARGET_SUN4
  141. #include <sun4.md/a.out.h>
  142. #elif TARGET_MACHINE==TARGET_SUN3
  143. #include <sun3.md/a.out.h>
  144. #endif
  145. #else
  146. #include <a.out.h>
  147. #endif
  148. #endif
  149.  
  150. #ifdef sprite
  151. #undef NEW_SEG_SIZE
  152. #if TARGET_MACHINE==TARGET_SUN4
  153. #define NEW_SEG_SIZE    0x40000
  154. #endif
  155. #if TARGET_MACHINE==TARGET_SUN3
  156. #define NEW_SEG_SIZE    0x20000
  157. #endif
  158. #endif  /* sprite */
  159.  
  160. #ifndef N_SET_MAGIC
  161. #define N_SET_MAGIC(exec, val)  ((exec).a_magic = val)
  162. #endif
  163.  
  164. /* If compiled with GNU C, use the built-in alloca */
  165. #ifdef __GNUC__
  166. #define alloca __builtin_alloca
  167. #endif
  168.  
  169. /* Always use the GNU version of debugging symbol type codes, if possible.  */
  170.  
  171. #include "stab.h"
  172. #define CORE_ADDR unsigned long    /* For symseg.h */
  173. #include "symseg.h"
  174.  
  175. #ifdef USG
  176. #include <string.h>
  177. #else
  178. #include <strings.h>
  179. #endif
  180.  
  181. /* Determine whether we should attempt to handle (minimally)
  182.    N_BINCL and N_EINCL.  */
  183.  
  184. #if defined (__GNU_STAB__) || defined (N_BINCL)
  185. #define HAVE_SUN_STABS
  186. #endif
  187.  
  188. #define min(a,b) ((a) < (b) ? (a) : (b))
  189.  
  190. /* Macro to control the number of undefined references printed */
  191. #define MAX_UREFS_PRINTED    10
  192.  
  193. /* Size of a page; obtained from the operating system.  */
  194.  
  195. int page_size;
  196.  
  197. /* Name this program was invoked by.  */
  198.  
  199. char *progname;
  200.  
  201. /* System dependencies */
  202.  
  203. /* Define this if names etext, edata and end should not start with `_'.  */
  204. /* #define nounderscore 1 */
  205.  
  206. /* Define NON_NATIVE if using BSD or pseudo-BSD file format on a system
  207.    whose native format is different.  */
  208. /* #define NON_NATIVE */
  209.  
  210. /* Define this to specify the default executable format.  */
  211.  
  212. #if TARGET_MACHINE==TARGET_HPUX
  213. #define DEFAULT_MAGIC NMAGIC  /* hpux bugs screw ZMAGIC */
  214. #endif
  215.  
  216. #ifndef DEFAULT_MAGIC
  217. #define DEFAULT_MAGIC ZMAGIC
  218. #endif
  219.  
  220. /* Ordinary 4.3bsd lacks these macros in a.out.h.  */
  221.  
  222. #ifndef N_TXTADDR
  223. #if TARGET_MACHINE==TARGET_VAX || TARGET_MACHINE==TARGET_SONY_NEWS
  224. #define N_TXTADDR(X) 0
  225. #endif
  226. #if TARGET_MACHINE==TARGET_SUN3 || TARGET_MACHINE==TARGET_SUN2
  227. #define N_TXTADDR(x)  (sizeof (struct exec))
  228. #endif
  229. #if TARGET_MAHCINE==TARGET_SEQUENT
  230. #define    N_TXTADDR(x) (N_ADDRADJ(x))
  231. #endif
  232. #endif
  233.  
  234. #ifndef N_DATADDR
  235. #if TARGET_MACHINE==TARGET_VAX || TARGET_MACHINE==TARGET_SONY_NEWS
  236. #define N_DATADDR(x) \
  237.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  238.     : (page_size+((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
  239. #endif
  240. #if TARGET_MACHINE==TARGET_SUN3 || TARGET_MACHINE==TARGET_SUN2
  241. #define SEGMENT_SIZE 0x20000
  242. #define N_DATADDR(x) \
  243.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  244.      : (SEGMENT_SIZE + ((N_TXTADDR(x)+(x).a_text-1) & ~(SEGMENT_SIZE-1))))
  245. #endif
  246. #if TARGET_MACHINE==TARGET_SEQUENT
  247. #define N_DATADDR(x) \
  248.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  249.     : (page_size+(((x).a_text-1) & ~(page_size-1))))
  250. #endif
  251. #endif
  252.  
  253. /* Define how to initialize system-dependent header fields.  */
  254. #if TARGET_MACHINE==TARGET_SUN4
  255. #define INITIALIZE_HEADER \
  256.   {outheader.a_machtype = M_SPARC; outheader.a_toolversion = 1;}
  257. #endif
  258. #if TARGET_MACHINE==TARGET_SUN2
  259. #define INITIALIZE_HEADER outheader.a_machtype = M_68010
  260. #endif
  261. #if TARGET_MACHINE==TARGET_SUN3
  262. #define INITIALIZE_HEADER outheader.a_machtype = M_68020
  263. #endif
  264. #if TARGET_MACHINE==TARGET_ALTOS
  265. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_68020)
  266. #endif
  267. #if TARGET_MACHINE==TARGET_HPUX
  268. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, HP9000S200_ID)
  269. #endif
  270. #if TARGET_MACHINE==TARGET_I386
  271. #define INITIALIZE_HEADER N_SET_MACHTYPE (outheader, M_386)
  272. #endif
  273. #if TARGET_MACHINE==TARGET_68K
  274. #define INITIALIZE_HEADER outheader.a_machtype = 0;  
  275. /* This enables code to take care of an ugly hack in the ISI OS.
  276.    If a symbol beings with _$, then the object file is included only
  277.    if the rest of the symbol name has been referenced. */
  278. #define DOLLAR_KLUDGE
  279. #endif
  280.  
  281. /*
  282.  * Alloca include.
  283.  */
  284. #if defined(sun) && defined(sparc) && !defined(__GNUC__)
  285. #include "alloca.h"
  286. #endif
  287.  
  288. #ifndef L_SET
  289. #define L_SET 0
  290. #endif
  291.  
  292. /*
  293.  * Ok.  Following are the relocation information macros.  If your
  294.  * system cannot use the default set (below), you must define all of these:
  295.  
  296.  *   relocation_info: This must be typedef'd (or #define'd) to the type
  297.  * of structure that is stored in the relocation info section of your
  298.  * a.out files.  Often this is defined in the a.out.h for your system.
  299.  *
  300.  *   RELOC_ADDRESS (rval): Offset into the current section of the
  301.  * <whatever> to be relocated.  *Must be an lvalue*.
  302.  *
  303.  *   RELOC_EXTERN_P (rval):  Is this relocation entry based on an
  304.  * external symbol (1), or was it fully resolved upon entering the
  305.  * loader (0) in which case some combination of the value in memory
  306.  * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains
  307.  * what the value of the relocation actually was.  *Must be an lvalue*.
  308.  *
  309.  *   RELOC_TYPE (rval): For a non-external relocation, this is the
  310.  * segment to relocate for.
  311.  *
  312.  *   RELOC_SYMBOL (rval): For an external relocation, this is the
  313.  * index of its symbol in the symbol table.  *Must be an lvalue*.
  314.  *
  315.  *   RELOC_MEMORY_ADD_P (rval): This should be 1 if the final
  316.  * relocation value output here should be added to memory; 0, if the
  317.  * section of memory described should simply be set to the relocation
  318.  * value.
  319.  *
  320.  *   RELOC_MEMORY_ADD_P (rval): If this is nonzero, the value previously
  321.  * present in the memory location to be relocated is *added*
  322.  * to the relocation value, to produce the final result.
  323.  * Otherwise, the relocation value is stored in the memory location
  324.  * and the value previously found there is ignored.
  325.  * By default, this is always 1.
  326.  *
  327.  *   RELOC_MEMORY_SUB_P (rval): If this is nonzero, the value previously
  328.  * present in the memory location to be relocated is *subtracted*
  329.  * from the relocation value, to produce the final result.
  330.  * By default, this is always 0.
  331.  *
  332.  *   RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives
  333.  * an extra value to be added to the relocation value based on the
  334.  * individual relocation entry.  *Must be an lvalue if defined*.
  335.  *
  336.  *   RELOC_PCREL_P (rval): True if the relocation value described is
  337.  * pc relative.
  338.  *
  339.  *   RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the
  340.  * final relocation value before putting it where it belongs.
  341.  *
  342.  *   RELOC_TARGET_SIZE (rval): log to the base 2 of the number of
  343.  * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes
  344.  * == 1; 4 bytes == 2, and etc.  This is somewhat redundant (we could
  345.  * do everything in terms of the bit operators below), but having this
  346.  * macro could end up producing better code on machines without fancy
  347.  * bit twiddling.  Also, it's easier to understand/code big/little
  348.  * endian distinctions with this macro.
  349.  *
  350.  *   RELOC_TARGET_BITPOS (rval): The starting bit position within the
  351.  * object described in RELOC_TARGET_SIZE in which the relocation value
  352.  * will go.
  353.  *
  354.  *   RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced
  355.  * with the bits of the relocation value.  It may be assumed by the
  356.  * code that the relocation value will fit into this many bits.  This
  357.  * may be larger than RELOC_TARGET_SIZE if such be useful.
  358.  *
  359.  *
  360.  *        Things I haven't implemented
  361.  *        ----------------------------
  362.  *
  363.  *    Values for RELOC_TARGET_SIZE other than 0, 1, or 2.
  364.  *
  365.  *    Pc relative relocation for External references.
  366.  *
  367.  *
  368.  */
  369.  
  370. #if TARGET_MACHINE==TARGET_SUN4
  371. /* Sparc (Sun 4) macros */
  372. #undef relocation_info
  373. #define relocation_info                    reloc_info_sparc
  374. #define RELOC_ADDRESS(r)        ((r)->r_address)                 
  375. #define RELOC_EXTERN_P(r)               ((r)->r_extern)      
  376. #define RELOC_TYPE(r)                   ((r)->r_index)  
  377. #define RELOC_SYMBOL(r)                 ((r)->r_index)   
  378. #define RELOC_MEMORY_SUB_P(r)        0
  379. #define RELOC_MEMORY_ADD_P(r)           0
  380. #define RELOC_ADD_EXTRA(r)              ((r)->r_addend)       
  381. #define RELOC_PCREL_P(r)                \
  382.     ((int) (r)->r_type >= (int) RELOC_DISP8 && \
  383.      (int) (r)->r_type <= (int) RELOC_WDISP22)
  384. #define RELOC_VALUE_RIGHTSHIFT(r) \
  385.     (reloc_target_rightshift[(int) (r)->r_type])
  386. #define RELOC_TARGET_SIZE(r)            (reloc_target_size[(int) (r)->r_type])
  387. #define RELOC_TARGET_BITPOS(r)          0
  388. #define RELOC_TARGET_BITSIZE(r) \
  389.     (reloc_target_bitsize[(int) (r)->r_type])
  390.  
  391. /* Note that these are very dependent on the order of the enums in
  392.    enum reloc_type (in a.out.h); if they change the following must be
  393.    changed */
  394. /* Also note that the last few may be incorrect; I have no information */
  395. static int reloc_target_rightshift[] = {
  396.   0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0,
  397. };
  398. static int reloc_target_size[] = {
  399.   0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  400. };
  401. static int reloc_target_bitsize[] = {
  402.   8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16,
  403. };
  404.  
  405. #define    MAX_ALIGNMENT    (sizeof (double))
  406.   /* The pagesize on an old sun4 is 0x2000, on sparcStation it is 0x1000.
  407.      If you want to use the same binaries on both, then you need to use
  408.      the larger pagesize. */
  409. #define TARGET_PAGE_SIZE    0x2000
  410. #endif
  411.  
  412. #if TARGET_MACHINE==TARGET_SEQUENT
  413. #define RELOC_ADDRESS(r)        ((r)->r_address)
  414. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  415. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  416. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  417. #define RELOC_MEMORY_SUB_P(r)    ((r)->r_bsr)
  418. #define RELOC_MEMORY_ADD_P(r)    1
  419. #undef RELOC_ADD_EXTRA
  420. #define RELOC_PCREL_P(r)        ((r)->r_pcrel || (r)->r_bsr)
  421. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  422. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  423. #define RELOC_TARGET_BITPOS(r)    0
  424. #define RELOC_TARGET_BITSIZE(r)    32
  425. #endif
  426.  
  427. /* Default macros */
  428. #ifndef RELOC_ADDRESS
  429. #define RELOC_ADDRESS(r)        ((r)->r_address)
  430. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  431. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  432. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  433. #define RELOC_MEMORY_SUB_P(r)    0
  434. #define RELOC_MEMORY_ADD_P(r)    1
  435. #undef RELOC_ADD_EXTRA
  436. #define RELOC_PCREL_P(r)        ((r)->r_pcrel)
  437. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  438. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  439. #define RELOC_TARGET_BITPOS(r)    0
  440. #define RELOC_TARGET_BITSIZE(r)    32
  441. #endif
  442.  
  443. #ifndef MAX_ALIGNMENT
  444. #define    MAX_ALIGNMENT    (sizeof (int))
  445. #endif
  446.  
  447. #ifdef nounderscore
  448. #define LPREFIX '.'
  449. #else
  450. #define LPREFIX 'L'
  451. #endif
  452.  
  453.  
  454. /* Special global symbol types understood by GNU LD.  */
  455.  
  456. /* The following type indicates the definition of a symbol as being
  457.    an indirect reference to another symbol.  The other symbol
  458.    appears as an undefined reference, immediately following this symbol.
  459.  
  460.    Indirection is asymmetrical.  The other symbol's value will be used
  461.    to satisfy requests for the indirect symbol, but not vice versa.
  462.    If the other symbol does not have a definition, libraries will
  463.    be searched to find a definition.
  464.  
  465.    So, for example, the following two lines placed in an assembler
  466.    input file would result in an object file which would direct gnu ld
  467.    to resolve all references to symbol "foo" as references to symbol
  468.    "bar". 
  469.  
  470.     .stabs "_foo",11,0,0,0
  471.     .stabs "_bar",1,0,0,0
  472.  
  473.    Note that (11 == (N_INDR | N_EXT)) and (1 == (N_UNDF | N_EXT)).  */
  474.  
  475. #ifndef N_INDR
  476. #define N_INDR 0xa
  477. #endif
  478.  
  479. /* The following symbols refer to set elements.  These are expected
  480.    only in input to the loader; they should not appear in loader
  481.    output (unless relocatable output is requested).  To be recognized
  482.    by the loader, the input symbols must have their N_EXT bit set.
  483.    All the N_SET[ATDB] symbols with the same name form one set.  The
  484.    loader collects all of these elements at load time and outputs a
  485.    vector for each name.
  486.    Space (an array of 32 bit words) is allocated for the set in the
  487.    data section, and the n_value field of each set element value is
  488.    stored into one word of the array.
  489.    The first word of the array is the length of the set (number of
  490.    elements).  The last word of the vector is set to zero for possible
  491.    use by incremental loaders.  The array is ordered by the linkage
  492.    order; the first symbols which the linker encounters will be first
  493.    in the array.
  494.  
  495.    In C syntax this looks like:
  496.  
  497.     struct set_vector {
  498.       unsigned int length;
  499.       unsigned int vector[length];
  500.       unsigned int always_zero;
  501.     };
  502.  
  503.    Before being placed into the array, each element is relocated
  504.    according to its type.  This allows the loader to create an array
  505.    of pointers to objects automatically.  N_SETA type symbols will not
  506.    be relocated.
  507.  
  508.    The address of the set is made into an N_SETV symbol
  509.    whose name is the same as the name of the set.
  510.    This symbol acts like a N_DATA global symbol
  511.    in that it can satisfy undefined external references.
  512.  
  513.    For the purposes of determining whether or not to load in a library
  514.    file, set element definitions are not considered "real
  515.    definitions"; they will not cause the loading of a library
  516.    member.
  517.  
  518.    If relocatable output is requested, none of this processing is
  519.    done.  The symbols are simply relocated and passed through to the
  520.    output file.
  521.  
  522.    So, for example, the following three lines of assembler code
  523.    (whether in one file or scattered between several different ones)
  524.    will produce a three element vector (total length is five words;
  525.    see above), referenced by the symbol "_xyzzy", which will have the
  526.    addresses of the routines _init1, _init2, and _init3.
  527.  
  528.    *NOTE*: If symbolic addresses are used in the n_value field of the
  529.    defining .stabs, those symbols must be defined in the same file as
  530.    that containing the .stabs.
  531.  
  532.     .stabs "_xyzzy",23,0,0,_init1
  533.     .stabs "_xyzzy",23,0,0,_init2
  534.     .stabs "_xyzzy",23,0,0,_init3
  535.  
  536.    Note that (23 == (N_SETT | N_EXT)).  */
  537.  
  538. #ifndef N_SETA
  539. #define    N_SETA    0x14        /* Absolute set element symbol */
  540. #endif                /* This is input to LD, in a .o file.  */
  541.  
  542. #ifndef N_SETT
  543. #define    N_SETT    0x16        /* Text set element symbol */
  544. #endif                /* This is input to LD, in a .o file.  */
  545.  
  546. #ifndef N_SETD
  547. #define    N_SETD    0x18        /* Data set element symbol */
  548. #endif                /* This is input to LD, in a .o file.  */
  549.  
  550. #ifndef N_SETB
  551. #define    N_SETB    0x1A        /* Bss set element symbol */
  552. #endif                /* This is input to LD, in a .o file.  */
  553.  
  554. /* Macros dealing with the set element symbols defined in a.out.h */
  555. #define    SET_ELEMENT_P(x)    ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
  556. #define TYPE_OF_SET_ELEMENT(x)    ((x)-N_SETA+N_ABS)
  557.  
  558. #ifndef N_SETV
  559. #define N_SETV    0x1C        /* Pointer to set vector in data area.  */
  560. #endif                /* This is output from LD.  */
  561.  
  562. /* If a this type of symbol is encountered, its name is a warning
  563.    message to print each time the symbol referenced by the next symbol
  564.    table entry is referenced.   
  565.  
  566.    This feature may be used to allow backwards compatibility with
  567.    certain functions (eg. gets) but to discourage programmers from
  568.    their use.
  569.  
  570.    So if, for example, you wanted to have ld print a warning whenever
  571.    the function "gets" was used in their C program, you would add the
  572.    following to the assembler file in which gets is defined:
  573.  
  574.     .stabs "Obsolete function \"gets\" referenced",30,0,0,0
  575.     .stabs "_gets",1,0,0,0
  576.  
  577.    These .stabs do not necessarily have to be in the same file as the
  578.    gets function, they simply must exist somewhere in the compilation.  */
  579.  
  580. #ifndef N_WARNING
  581. #define N_WARNING 0x1E        /* Warning message to print if symbol
  582.                    included */
  583. #endif                /* This is input to ld */
  584.  
  585. #ifndef __GNU_STAB__
  586.  
  587. /* Line number for the data section.  This is to be used to describe
  588.    the source location of a variable declaration.  */
  589. #ifndef N_DSLINE
  590. #define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
  591. #endif
  592.  
  593. /* Line number for the bss section.  This is to be used to describe
  594.    the source location of a variable declaration.  */
  595. #ifndef N_BSLINE
  596. #define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
  597. #endif
  598.  
  599. #endif /* not __GNU_STAB__ */
  600.  
  601. /* Symbol table */
  602.  
  603. /* Global symbol data is recorded in these structures,
  604.    one for each global symbol.
  605.    They are found via hashing in 'symtab', which points to a vector of buckets.
  606.    Each bucket is a chain of these structures through the link field.  */
  607.  
  608. typedef
  609.   struct glosym
  610.     {
  611.       /* Pointer to next symbol in this symbol's hash bucket.  */
  612.       struct glosym *link;
  613.       /* Name of this symbol.  */
  614.       char *name;
  615.       /* Value of this symbol as a global symbol.  */
  616.       long value;
  617.       /* Chain of external 'nlist's in files for this symbol, both defs
  618.      and refs.  */
  619.       struct nlist *refs;
  620.       /* Any warning message that might be associated with this symbol
  621.          from an N_WARNING symbol encountered. */
  622.       char *warning;
  623.       /* Nonzero means definitions of this symbol as common have been seen,
  624.      and the value here is the largest size specified by any of them.  */
  625.       int max_common_size;
  626.       /* For relocatable_output, records the index of this global sym in the
  627.      symbol table to be written, with the first global sym given index 0.*/
  628.       int def_count;
  629.       /* Nonzero means a definition of this global symbol is known to exist.
  630.      Library members should not be loaded on its account.  */
  631.       char defined;
  632.       /* Nonzero means a reference to this global symbol has been seen
  633.      in a file that is surely being loaded.
  634.      A value higher than 1 is the n_type code for the symbol's
  635.      definition.  */
  636.       char referenced;
  637.       /* A count of the number of undefined references printed for a
  638.      specific symbol.  If a symbol is unresolved at the end of
  639.      digest_symbols (and the loading run is supposed to produce
  640.      relocatable output) do_file_warnings keeps track of how many
  641.      unresolved reference error messages have been printed for
  642.      each symbol here.  When the number hits MAX_UREFS_PRINTED,
  643.      messages stop. */
  644.       unsigned char undef_refs;
  645.       /* 1 means that this symbol has multiple definitions.  2 means
  646.          that it has multiple definitions, and some of them are set
  647.      elements, one of which has been printed out already.  */
  648.       unsigned char multiply_defined;
  649.       /* Nonzero means print a message at all refs or defs of this symbol */
  650.       char trace;
  651.     }
  652.   symbol;
  653.  
  654. /* Number of buckets in symbol hash table */
  655. #define    TABSIZE    1009
  656.  
  657. /* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */
  658. symbol *symtab[TABSIZE];
  659.  
  660. /* Number of symbols in symbol hash table. */
  661. int num_hash_tab_syms = 0;
  662.  
  663. /* Count the number of nlist entries that are for local symbols.
  664.    This count and the three following counts
  665.    are incremented as as symbols are entered in the symbol table.  */
  666. int local_sym_count;
  667.  
  668. /* Count number of nlist entries that are for local symbols
  669.    whose names don't start with L. */
  670. int non_L_local_sym_count;
  671.  
  672. /* Count the number of nlist entries for debugger info.  */
  673. int debugger_sym_count;
  674.  
  675. /* Count the number of global symbols referenced and not defined.  */
  676. int undefined_global_sym_count;
  677.  
  678. /* Count the number of global symbols multiply defined.  */
  679. int multiple_def_count;
  680.  
  681. /* Count the number of defined global symbols.
  682.    Each symbol is counted only once
  683.    regardless of how many different nlist entries refer to it,
  684.    since the output file will need only one nlist entry for it.
  685.    This count is computed by `digest_symbols';
  686.    it is undefined while symbols are being loaded. */
  687. int defined_global_sym_count;
  688.  
  689. /* Count the number of symbols defined through common declarations.
  690.    This count is kept in symdef_library, linear_library, and
  691.    enter_global_ref.  It is incremented when the defined flag is set
  692.    in a symbol because of a common definition, and decremented when
  693.    the symbol is defined "for real" (ie. by something besides a common
  694.    definition).  */
  695. int common_defined_global_count;
  696.  
  697. /* Count the number of set element type symbols and the number of
  698.    separate vectors which these symbols will fit into.  See the
  699.    GNU a.out.h for more info.
  700.    This count is computed by 'enter_file_symbols' */
  701. int set_symbol_count;
  702. int set_vector_count;
  703.  
  704. /* Define a linked list of strings which define symbols which should
  705.    be treated as set elements even though they aren't.  Any symbol
  706.    with a prefix matching one of these should be treated as a set
  707.    element.
  708.  
  709.    This is to make up for deficiencies in many assemblers which aren't
  710.    willing to pass any stabs through to the loader which they don't
  711.    understand.  */
  712. struct string_list_element {
  713.   char *str;
  714.   struct string_list_element *next;
  715. };
  716.  
  717. struct string_list_element *set_element_prefixes;
  718.  
  719. /* Count the number of definitions done indirectly (ie. done relative
  720.    to the value of some other symbol. */
  721. int global_indirect_count;
  722.  
  723. /* Count the number of warning symbols encountered. */
  724. int warning_count;
  725.  
  726. /* Total number of symbols to be written in the output file.
  727.    Computed by digest_symbols from the variables above.  */
  728. int nsyms;
  729.  
  730.  
  731. /* Nonzero means ptr to symbol entry for symbol to use as start addr.
  732.    -e sets this.  */
  733. symbol *entry_symbol;
  734.  
  735. symbol *edata_symbol;   /* the symbol _edata */
  736. symbol *etext_symbol;   /* the symbol _etext */
  737. symbol *end_symbol;    /* the symbol _end */
  738.  
  739. /* Each input file, and each library member ("subfile") being loaded,
  740.    has a `file_entry' structure for it.
  741.  
  742.    For files specified by command args, these are contained in the vector
  743.    which `file_table' points to.
  744.  
  745.    For library members, they are dynamically allocated,
  746.    and chained through the `chain' field.
  747.    The chain is found in the `subfiles' field of the `file_entry'.
  748.    The `file_entry' objects for the members have `superfile' fields pointing
  749.    to the one for the library.  */
  750.  
  751. struct file_entry {
  752.   /* Name of this file.  */
  753.   char *filename;
  754.   /* Name to use for the symbol giving address of text start */
  755.   /* Usually the same as filename, but for a file spec'd with -l
  756.      this is the -l switch itself rather than the filename.  */
  757.   char *local_sym_name;
  758.  
  759.   /* Describe the layout of the contents of the file */
  760.  
  761.   /* The file's a.out header.  */
  762.   struct exec header;
  763.   /* Offset in file of GDB symbol segment, or 0 if there is none.  */
  764.   int symseg_offset;
  765.  
  766.   /* Describe data from the file loaded into core */
  767.  
  768.   /* Symbol table of the file.  */
  769.   struct nlist *symbols;
  770.   /* Size in bytes of string table.  */
  771.   int string_size;
  772.   /* Pointer to the string table.
  773.      The string table is not kept in core all the time,
  774.      but when it is in core, its address is here.  */
  775.   char *strings;
  776.  
  777.   /* Next two used only if `relocatable_output' or if needed for */
  778.   /* output of undefined reference line numbers. */
  779.  
  780.   /* Text reloc info saved by `write_text' for `coptxtrel'.  */
  781.   struct relocation_info *textrel;
  782.   /* Data reloc info saved by `write_data' for `copdatrel'.  */
  783.   struct relocation_info *datarel;
  784.  
  785.   /* Relation of this file's segments to the output file */
  786.  
  787.   /* Start of this file's text seg in the output file core image.  */
  788.   int text_start_address;
  789.   /* Start of this file's data seg in the output file core image.  */
  790.   int data_start_address;
  791.   /* Start of this file's bss seg in the output file core image.  */
  792.   int bss_start_address;
  793.   /* Offset in bytes in the output file symbol table
  794.      of the first local symbol for this file.  Set by `write_file_symbols'.  */
  795.   int local_syms_offset;
  796.  
  797.   /* For library members only */
  798.  
  799.   /* For a library, points to chain of entries for the library members.  */
  800.   struct file_entry *subfiles;
  801.   /* For a library member, offset of the member within the archive.
  802.      Zero for files that are not library members.  */
  803.   int starting_offset;
  804.   /* Size of contents of this file, if library member.  */
  805.   int total_size;
  806.   /* For library member, points to the library's own entry.  */
  807.   struct file_entry *superfile;
  808.   /* For library member, points to next entry for next member.  */
  809.   struct file_entry *chain;
  810.  
  811.   /* 1 if file is a library. */
  812.   char library_flag;
  813.  
  814.   /* 1 if file's header has been read into this structure.  */
  815.   char header_read_flag;
  816.  
  817.   /* 1 means search a set of directories for this file.  */
  818.   char search_dirs_flag;
  819.  
  820.   /* 1 means this is base file of incremental load.
  821.      Do not load this file's text or data.
  822.      Also default text_start to after this file's bss. */
  823.   char just_syms_flag;
  824. };
  825.  
  826. /* Vector of entries for input files specified by arguments.
  827.    These are all the input files except for members of specified libraries.  */
  828. struct file_entry *file_table;
  829.  
  830. /* Length of that vector.  */
  831. int number_of_files;
  832.  
  833. /* When loading the text and data, we can avoid doing a close
  834.    and another open between members of the same library.
  835.  
  836.    These two variables remember the file that is currently open.
  837.    Both are zero if no file is open.
  838.  
  839.    See `each_file' and `file_close'.  */
  840.  
  841. struct file_entry *input_file;
  842. int input_desc;
  843.  
  844. /* The name of the file to write; "a.out" by default.  */
  845.  
  846. char *output_filename;
  847.  
  848. /* Descriptor for writing that file with `mywrite'.  */
  849.  
  850. int outdesc;
  851.  
  852. /* Header for that file (filled in by `write_header').  */
  853.  
  854. struct exec outheader;
  855.  
  856. #ifdef COFF_ENCAPSULATE
  857. struct coffheader coffheader;
  858. int need_coff_header;
  859. #endif
  860.  
  861. /* The following are computed by `digest_symbols'.  */
  862.  
  863. int text_size;        /* total size of text of all input files.  */
  864. int data_size;        /* total size of data of all input files.  */
  865. int bss_size;        /* total size of bss of all input files.  */
  866. int text_reloc_size;    /* total size of text relocation of all input files.  */
  867. int data_reloc_size;    /* total size of data relocation of all input */
  868.             /* files.  */
  869.  
  870. /* Specifications of start and length of the area reserved at the end
  871.    of the text segment for the set vectors.  Computed in 'digest_symbols' */
  872. int set_sect_start;
  873. int set_sect_size;
  874.  
  875. /* Pointer for in core storage for the above vectors, before they are
  876.    written. */
  877. unsigned long *set_vectors;
  878.  
  879. /* Amount of cleared space to leave between the text and data segments.  */
  880.  
  881. int text_pad;
  882.  
  883. /* Amount of bss segment to include as part of the data segment.  */
  884.  
  885. int data_pad;
  886.  
  887. /* Format of __.SYMDEF:
  888.    First, a longword containing the size of the 'symdef' data that follows.
  889.    Second, zero or more 'symdef' structures.
  890.    Third, a longword containing the length of symbol name strings.
  891.    Fourth, zero or more symbol name strings (each followed by a null).  */
  892.  
  893. struct symdef {
  894.   int symbol_name_string_index;
  895.   int library_member_offset;
  896. };
  897.  
  898. /* Record most of the command options.  */
  899.  
  900. /* Address we assume the text section will be loaded at.
  901.    We relocate symbols and text and data for this, but we do not
  902.    write any padding in the output file for it.  */
  903. int text_start;
  904.  
  905. /* Offset of default entry-pc within the text section.  */
  906. int entry_offset;
  907.  
  908. /* Address we decide the data section will be loaded at.  */
  909. int data_start;
  910.  
  911. /* `text-start' address is normally this much plus a page boundary.
  912.    This is not a user option; it is fixed for each system.  */
  913. int text_start_alignment;
  914.  
  915. /* Nonzero if -T was specified in the command line.
  916.    This prevents text_start from being set later to default values.  */
  917. int T_flag_specified;
  918.  
  919. /* Nonzero if -Tdata was specified in the command line.
  920.    This prevents data_start from being set later to default values.  */
  921. int Tdata_flag_specified;
  922.  
  923. /* Size to pad data section up to.
  924.    We simply increase the size of the data section, padding with zeros,
  925.    and reduce the size of the bss section to match.  */
  926. int specified_data_size;
  927.  
  928. /* Magic number to use for the output file, set by switch.  */
  929. int magic;
  930.  
  931. /* Nonzero means print names of input files as processed.  */
  932. int trace_files;
  933.  
  934. /* Which symbols should be stripped (omitted from the output):
  935.    none, all, or debugger symbols.  */
  936. enum { STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER } strip_symbols;
  937.  
  938. /* Which local symbols should be omitted:
  939.    none, all, or those starting with L.
  940.    This is irrelevant if STRIP_NONE.  */
  941. enum { DISCARD_NONE, DISCARD_ALL, DISCARD_L } discard_locals;
  942.  
  943. /* 1 => write load map.  */
  944. int write_map;
  945.  
  946. /* 1 => write relocation into output file so can re-input it later.  */
  947. int relocatable_output;
  948.  
  949. /* 1 => assign space to common symbols even if `relocatable_output'.  */
  950. int force_common_definition;
  951.  
  952. /* Standard directories to search for files specified by -l.  */
  953. char *standard_search_dirs[] =
  954. #ifdef STANDARD_SEARCH_DIRS
  955.   {STANDARD_SEARCH_DIRS};
  956. #else
  957. #ifdef NON_NATIVE
  958.   {"/usr/local/lib/gnu"};
  959. #else
  960.   {"/lib", "/usr/lib", "/usr/local/lib"};
  961. #endif
  962. #endif
  963.  
  964. /* Actual vector of directories to search;
  965.    this contains those specified with -L plus the standard ones.  */
  966. char **search_dirs;
  967.  
  968. /* Length of the vector `search_dirs'.  */
  969. int n_search_dirs;
  970.  
  971. /* Non zero means to create the output executable. */
  972. /* Cleared by nonfatal errors.  */
  973. int make_executable;
  974.  
  975. /* Force the executable to be output, even if there are non-fatal
  976.    errors */
  977. int force_executable;
  978.  
  979. /* Keep a list of any symbols referenced from the command line (so
  980.    that error messages for these guys can be generated). This list is
  981.    zero terminated. */
  982. struct glosym **cmdline_references;
  983. int cl_refs_allocated;
  984.  
  985. void bcopy (), bzero ();
  986. int malloc (), realloc ();
  987. #ifndef alloca
  988. int alloca ();
  989. #endif
  990. int free ();
  991.  
  992. int xmalloc ();
  993. int xrealloc ();
  994. void fatal ();
  995. void fatal_with_file ();
  996. void perror_name ();
  997. void perror_file ();
  998. void error ();
  999.  
  1000. void digest_symbols ();
  1001. void print_symbols ();
  1002. void load_symbols ();
  1003. void decode_command ();
  1004. void list_undefined_symbols ();
  1005. void list_unresolved_references ();
  1006. void write_output ();
  1007. void write_header ();
  1008. void write_text ();
  1009. void read_file_relocation ();
  1010. void write_data ();
  1011. void write_rel ();
  1012. void write_syms ();
  1013. void write_symsegs ();
  1014. void mywrite ();
  1015. void symtab_init ();
  1016. void padfile ();
  1017. char *concat ();
  1018. char *get_file_name ();
  1019. symbol *getsym (), *getsym_soft ();
  1020.  
  1021. int
  1022. main (argc, argv)
  1023.      char **argv;
  1024.      int argc;
  1025. {
  1026.  
  1027. #ifdef TARGET_PAGE_SIZE
  1028.   page_size = TARGET_PAGE_SIZE;
  1029. #else
  1030.   page_size = getpagesize ();
  1031. #endif
  1032.  
  1033.   progname = argv[0];
  1034.  
  1035.   /* Clear the cumulative info on the output file.  */
  1036.   text_size = 0;
  1037.   data_size = 0;
  1038.   bss_size = 0;
  1039.   text_reloc_size = 0;
  1040.   data_reloc_size = 0;
  1041.  
  1042.   data_pad = 0;
  1043.   text_pad = 0;
  1044.  
  1045.   /* Initialize the data about options.  */
  1046.  
  1047.   specified_data_size = 0;
  1048.   strip_symbols = STRIP_NONE;
  1049.   trace_files = 0;
  1050.   discard_locals = DISCARD_NONE;
  1051.   entry_symbol = 0;
  1052.   write_map = 0;
  1053.   relocatable_output = 0;
  1054.   force_common_definition = 0;
  1055.   T_flag_specified = 0;
  1056.   Tdata_flag_specified = 0;
  1057.   magic = DEFAULT_MAGIC;
  1058.   make_executable = 1;
  1059.   force_executable = 0;
  1060.   set_element_prefixes = 0;
  1061.  
  1062.   /* Initialize the cumulative counts of symbols.  */
  1063.  
  1064.   local_sym_count = 0;
  1065.   non_L_local_sym_count = 0;
  1066.   debugger_sym_count = 0;
  1067.   undefined_global_sym_count = 0;
  1068.   set_symbol_count = 0;
  1069.   set_vector_count = 0;
  1070.   global_indirect_count = 0;
  1071.   warning_count = 0;
  1072.   multiple_def_count = 0;
  1073.   common_defined_global_count = 0;
  1074.  
  1075.   /* Keep a list of symbols referenced from the command line */
  1076.   cl_refs_allocated = 10;
  1077.   cmdline_references
  1078.     = (struct glosym **) xmalloc (cl_refs_allocated
  1079.                   * sizeof(struct glosym *));
  1080.   *cmdline_references = 0;
  1081.  
  1082.   /* Completely decode ARGV.  */
  1083.  
  1084.   decode_command (argc, argv);
  1085.  
  1086.   /* Create the symbols `etext', `edata' and `end'.  */
  1087.  
  1088.   if (!relocatable_output)
  1089.     symtab_init ();
  1090.  
  1091.   /* Determine whether to count the header as part of
  1092.      the text size, and initialize the text size accordingly.
  1093.      This depends on the kind of system and on the output format selected.  */
  1094.  
  1095.   N_SET_MAGIC (outheader, magic);
  1096. #ifdef INITIALIZE_HEADER
  1097.   INITIALIZE_HEADER;
  1098. #endif
  1099.  
  1100.   text_size = sizeof (struct exec);
  1101. #ifdef COFF_ENCAPSULATE
  1102.   if (relocatable_output == 0)
  1103.     {
  1104.       need_coff_header = 1;
  1105.       /* set this flag now, since it will change the values of N_TXTOFF, etc */
  1106.       N_SET_FLAGS (outheader, N_FLAGS_COFF_ENCAPSULATE);
  1107.       text_size += sizeof (struct coffheader);
  1108.     }
  1109. #endif
  1110.  
  1111.   text_size -= N_TXTOFF (outheader);
  1112.  
  1113.   if (text_size < 0)
  1114.     text_size = 0;
  1115.   entry_offset = text_size;
  1116.  
  1117.   if (!T_flag_specified && !relocatable_output)
  1118.     text_start = N_TXTADDR (outheader);
  1119.  
  1120.   /* The text-start address is normally this far past a page boundary.  */
  1121.   text_start_alignment = text_start % page_size;
  1122.  
  1123.   /* Load symbols of all input files.
  1124.      Also search all libraries and decide which library members to load.  */
  1125.  
  1126.   load_symbols ();
  1127.  
  1128.   /* Compute where each file's sections go, and relocate symbols.  */
  1129.  
  1130.   digest_symbols ();
  1131.  
  1132.   /* Print error messages for any missing symbols, for any warning
  1133.      symbols, and possibly multiple definitions */
  1134.  
  1135.   do_warnings (stderr);
  1136.  
  1137.   /* Print a map, if requested.  */
  1138.  
  1139.   if (write_map) print_symbols (stdout);
  1140.  
  1141.   /* Write the output file.  */
  1142.  
  1143.   if (make_executable || force_executable)
  1144.     write_output ();
  1145.  
  1146.   exit (!make_executable);
  1147. }
  1148.  
  1149. void decode_option ();
  1150.  
  1151. /* Analyze a command line argument.
  1152.    Return 0 if the argument is a filename.
  1153.    Return 1 if the argument is a option complete in itself.
  1154.    Return 2 if the argument is a option which uses an argument.
  1155.  
  1156.    Thus, the value is the number of consecutive arguments
  1157.    that are part of options.  */
  1158.  
  1159. int
  1160. classify_arg (arg)
  1161.      register char *arg;
  1162. {
  1163.   if (*arg != '-') return 0;
  1164.   switch (arg[1])
  1165.     {
  1166.     case 'A':
  1167.     case 'D':
  1168.     case 'e':
  1169.     case 'L':
  1170.     case 'l':
  1171.     case 'o':
  1172.     case 'u':
  1173.     case 'V':
  1174.     case 'y':
  1175.       if (arg[2])
  1176.     return 1;
  1177.       return 2;
  1178.  
  1179.     case 'B':
  1180.       if (! strcmp (&arg[2], "static"))
  1181.     return 1;
  1182.  
  1183.     case 'T':
  1184.       if (arg[2] == 0)
  1185.     return 2;
  1186.       if (! strcmp (&arg[2], "text"))
  1187.     return 2;
  1188.       if (! strcmp (&arg[2], "data"))
  1189.     return 2;
  1190.       return 1;
  1191.     }
  1192.  
  1193.   return 1;
  1194. }
  1195.  
  1196. /* Process the command arguments,
  1197.    setting up file_table with an entry for each input file,
  1198.    and setting variables according to the options.  */
  1199.  
  1200. void
  1201. decode_command (argc, argv)
  1202.      char **argv;
  1203.      int argc;
  1204. {
  1205.   register int i;
  1206.   register struct file_entry *p;
  1207.  
  1208.   number_of_files = 0;
  1209.   output_filename = "a.out";
  1210.  
  1211.   n_search_dirs = 0;
  1212.   search_dirs = (char **) xmalloc (sizeof (char *));
  1213.  
  1214.   /* First compute number_of_files so we know how long to make file_table.  */
  1215.   /* Also process most options completely.  */
  1216.  
  1217.   for (i = 1; i < argc; i++)
  1218.     {
  1219.       register int code = classify_arg (argv[i]);
  1220.       if (code)
  1221.     {
  1222.       if (i + code > argc)
  1223.         fatal ("no argument following %s\n", argv[i]);
  1224.  
  1225.       decode_option (argv[i], argv[i+1]);
  1226.  
  1227.       if (argv[i][1] == 'l' || argv[i][1] == 'A')
  1228.         number_of_files++;
  1229.  
  1230.       i += code - 1;
  1231.     }
  1232.       else
  1233.     number_of_files++;
  1234.     }
  1235.  
  1236.   if (!number_of_files)
  1237.     fatal ("no input files", 0);
  1238.  
  1239.   p = file_table
  1240.     = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  1241.   bzero (p, number_of_files * sizeof (struct file_entry));
  1242.  
  1243.   /* Now scan again and fill in file_table.  */
  1244.   /* All options except -A and -l are ignored here.  */
  1245.  
  1246.   for (i = 1; i < argc; i++)
  1247.     {
  1248.       register int code = classify_arg (argv[i]);
  1249.  
  1250.       if (code)
  1251.     {
  1252.       char *string;
  1253.       if (code == 2)
  1254.         string = argv[i+1];
  1255.       else
  1256.         string = &argv[i][2];
  1257.  
  1258.       if (argv[i][1] == 'A')
  1259.         {
  1260.           if (p != file_table)
  1261.         fatal ("-A specified before an input file other than the first");
  1262.  
  1263.           p->filename = string;
  1264.           p->local_sym_name = string;
  1265.           p->just_syms_flag = 1;
  1266.           p++;
  1267.         }
  1268.       if (argv[i][1] == 'l')
  1269.         {
  1270.           p->filename = concat ("lib", string, ".a");
  1271.           p->local_sym_name = concat ("-l", string, "");
  1272.           p->search_dirs_flag = 1;
  1273.           p++;
  1274.         }
  1275.       i += code - 1;
  1276.     }
  1277.       else
  1278.     {
  1279.       p->filename = argv[i];
  1280.       p->local_sym_name = argv[i];
  1281.       p++;
  1282.     }
  1283.     }
  1284.  
  1285.   /* Now check some option settings for consistency.  */
  1286.  
  1287. #ifndef sprite
  1288. #ifdef NMAGIC
  1289.   if ((magic == ZMAGIC || magic == NMAGIC)
  1290. #else
  1291.   if ((magic == ZMAGIC)
  1292. #endif
  1293.       && (text_start - text_start_alignment) & (page_size - 1))
  1294.     fatal ("-T argument not multiple of page size, with sharable output", 0);
  1295. #endif
  1296.  
  1297.   /* Append the standard search directories to the user-specified ones.  */
  1298.   {
  1299.     int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
  1300.     n_search_dirs += n;
  1301.     search_dirs
  1302.       = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1303.     bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
  1304.        n * sizeof (char *));
  1305.   }
  1306. }
  1307.  
  1308.  
  1309. void
  1310. add_cmdline_ref (sp)
  1311.      struct glosym *sp;
  1312. {
  1313.   struct glosym **ptr;
  1314.  
  1315.   for (ptr = cmdline_references;
  1316.        ptr < cmdline_references + cl_refs_allocated && *ptr;
  1317.        ptr++)
  1318.     ;
  1319.  
  1320.   if (ptr >= cmdline_references + cl_refs_allocated - 1)
  1321.     {
  1322.       int diff = ptr - cmdline_references;
  1323.       
  1324.       cl_refs_allocated *= 2;
  1325.       cmdline_references = (struct glosym **)
  1326.     xrealloc (cmdline_references,
  1327.          cl_refs_allocated * sizeof (struct glosym *));
  1328.       ptr = cmdline_references + diff;
  1329.     }
  1330.   
  1331.   *ptr++ = sp;
  1332.   *ptr = (struct glosym *) 0;
  1333. }
  1334.     
  1335. int
  1336. set_element_prefixed_p (name)
  1337.      char *name;
  1338. {
  1339.   struct string_list_element *p;
  1340.   int i;
  1341.  
  1342.   for (p = set_element_prefixes; p; p = p->next)
  1343.     {
  1344.       for (i = 0; p->str[i] != '\0' && (p->str[i] == name[i]); i++)
  1345.     ;
  1346.  
  1347.       if (p->str[i] == '\0')
  1348.     return 1;
  1349.     }
  1350.   return 0;
  1351. }
  1352.  
  1353. int parse ();
  1354.  
  1355. /* Record an option and arrange to act on it later.
  1356.    ARG should be the following command argument,
  1357.    which may or may not be used by this option.
  1358.  
  1359.    The `l' and `A' options are ignored here since they actually
  1360.    specify input files.  */
  1361.  
  1362. void
  1363. decode_option (swt, arg)
  1364.      register char *swt, *arg;
  1365. {
  1366.   /* We get Bstatic from gcc on suns.  */
  1367.   if (! strcmp (swt + 1, "Bstatic"))
  1368.     return;
  1369.   if (! strcmp (swt + 1, "Ttext"))
  1370.     {
  1371. #ifdef sprite 
  1372.       magic = OMAGIC;
  1373. #endif
  1374.       text_start = parse (arg, "%x", "invalid argument to -Ttext");
  1375.       T_flag_specified = 1;
  1376.       return;
  1377.     }
  1378.   if (! strcmp (swt + 1, "Tdata"))
  1379.     {
  1380.       data_start = parse (arg, "%x", "invalid argument to -Tdata");
  1381.       Tdata_flag_specified = 1;
  1382.       return;
  1383.     }
  1384.   if (! strcmp (swt + 1, "noinhibit-exec"))
  1385.     {
  1386.       force_executable = 1;
  1387.       return;
  1388.     }
  1389.  
  1390.   if (swt[2] != 0)
  1391.     arg = &swt[2];
  1392.  
  1393.   switch (swt[1])
  1394.     {
  1395.     case 'A':
  1396.       return;
  1397.  
  1398.     case 'D':
  1399.       specified_data_size = parse (arg, "%x", "invalid argument to -D");
  1400.       return;
  1401.  
  1402.     case 'd':
  1403.       force_common_definition = 1;
  1404.       return;
  1405.  
  1406.     case 'e':
  1407.       entry_symbol = getsym (arg);
  1408.       if (!entry_symbol->defined && !entry_symbol->referenced)
  1409.     undefined_global_sym_count++;
  1410.       entry_symbol->referenced = 1;
  1411.       add_cmdline_ref (entry_symbol);
  1412.       return;
  1413.  
  1414.     case 'l':
  1415.       return;
  1416.  
  1417.     case 'L':
  1418.       n_search_dirs++;
  1419.       search_dirs
  1420.     = (char **) xrealloc (search_dirs, n_search_dirs * sizeof (char *));
  1421.       search_dirs[n_search_dirs - 1] = arg;
  1422.       return;
  1423.  
  1424.     case 'M':
  1425.       write_map = 1;
  1426.       return;
  1427.  
  1428.     case 'N':
  1429.       magic = OMAGIC;
  1430.       return;
  1431.  
  1432. #ifdef NMAGIC
  1433.     case 'n':
  1434.       magic = NMAGIC;
  1435.       return;
  1436. #endif
  1437.  
  1438.     case 'o':
  1439.       output_filename = arg;
  1440.       return;
  1441.  
  1442.     case 'r':
  1443.       relocatable_output = 1;
  1444.       magic = OMAGIC;
  1445.       text_start = 0;
  1446.       return;
  1447.  
  1448.     case 'S':
  1449.       strip_symbols = STRIP_DEBUGGER;
  1450.       return;
  1451.  
  1452.     case 's':
  1453.       strip_symbols = STRIP_ALL;
  1454.       return;
  1455.  
  1456.     case 'T':
  1457. #ifdef sprite
  1458.       magic = OMAGIC;
  1459. #endif      
  1460.       text_start = parse (arg, "%x", "invalid argument to -T");
  1461.       T_flag_specified = 1;
  1462.       return;
  1463.  
  1464.     case 't':
  1465.       trace_files = 1;
  1466.       return;
  1467.  
  1468.     case 'u':
  1469.       {
  1470.     register symbol *sp = getsym (arg);
  1471.     if (!sp->defined && !sp->referenced)
  1472.       undefined_global_sym_count++;
  1473.     sp->referenced = 1;
  1474.     add_cmdline_ref (sp);
  1475.       }
  1476.       return;
  1477.  
  1478.     case 'V':
  1479.       {
  1480.     struct string_list_element *new
  1481.       = (struct string_list_element *)
  1482.         xmalloc (sizeof (struct string_list_element));
  1483.  
  1484.     new->str = arg;
  1485.     new->next = set_element_prefixes;
  1486.     set_element_prefixes = new;
  1487.     return;
  1488.       }
  1489.  
  1490.     case 'X':
  1491.       discard_locals = DISCARD_L;
  1492.       return;
  1493.  
  1494.     case 'x':
  1495.       discard_locals = DISCARD_ALL;
  1496.       return;
  1497.  
  1498.     case 'y':
  1499.       {
  1500.     register symbol *sp = getsym (&swt[2]);
  1501.     sp->trace = 1;
  1502.       }
  1503.       return;
  1504.  
  1505.     case 'z':
  1506.       magic = ZMAGIC;
  1507.       return;
  1508.  
  1509.     default:
  1510.       fatal ("invalid command option `%s'", swt);
  1511.     }
  1512. }
  1513.  
  1514. /** Convenient functions for operating on one or all files being */
  1515.  /** loaded.  */
  1516. void print_file_name ();
  1517.  
  1518. /* Call FUNCTION on each input file entry.
  1519.    Do not call for entries for libraries;
  1520.    instead, call once for each library member that is being loaded.
  1521.  
  1522.    FUNCTION receives two arguments: the entry, and ARG.  */
  1523.  
  1524. void
  1525. each_file (function, arg)
  1526.      register void (*function)();
  1527.      register int arg;
  1528. {
  1529.   register int i;
  1530.  
  1531.   for (i = 0; i < number_of_files; i++)
  1532.     {
  1533.       register struct file_entry *entry = &file_table[i];
  1534.       if (entry->library_flag)
  1535.         {
  1536.       register struct file_entry *subentry = entry->subfiles;
  1537.       for (; subentry; subentry = subentry->chain)
  1538.         (*function) (subentry, arg);
  1539.     }
  1540.       else
  1541.     (*function) (entry, arg);
  1542.     }
  1543. }
  1544.  
  1545. /* Call FUNCTION on each input file entry until it returns a non-zero
  1546.    value.  Return this value.
  1547.    Do not call for entries for libraries;
  1548.    instead, call once for each library member that is being loaded.
  1549.  
  1550.    FUNCTION receives two arguments: the entry, and ARG.  It must be a
  1551.    function returning unsigned long (though this can probably be fudged). */
  1552. #if 0
  1553. unsigned long
  1554. check_each_file (function, arg)
  1555.      register unsigned long (*function)();
  1556.      register int arg;
  1557. {
  1558.   register int i;
  1559.   register unsigned long return_val;
  1560.  
  1561.   for (i = 0; i < number_of_files; i++)
  1562.     {
  1563.       register struct file_entry *entry = &file_table[i];
  1564.       if (entry->library_flag)
  1565.         {
  1566.       register struct file_entry *subentry = entry->subfiles;
  1567.       for (; subentry; subentry = subentry->chain)
  1568.         if (return_val = (*function) (subentry, arg))
  1569.           return return_val;
  1570.     }
  1571.       else
  1572.     if (return_val = (*function) (entry, arg))
  1573.       return return_val;
  1574.     }
  1575.   return 0;
  1576. }
  1577. #endif
  1578.  
  1579. /* Like `each_file' but ignore files that were just for symbol definitions.  */
  1580.  
  1581. void
  1582. each_full_file (function, arg)
  1583.      register void (*function)();
  1584.      register int arg;
  1585. {
  1586.   register int i;
  1587.  
  1588.   for (i = 0; i < number_of_files; i++)
  1589.     {
  1590.       register struct file_entry *entry = &file_table[i];
  1591.       if (entry->just_syms_flag)
  1592.     continue;
  1593.       if (entry->library_flag)
  1594.         {
  1595.       register struct file_entry *subentry = entry->subfiles;
  1596.       for (; subentry; subentry = subentry->chain)
  1597.         (*function) (subentry, arg);
  1598.     }
  1599.       else
  1600.     (*function) (entry, arg);
  1601.     }
  1602. }
  1603.  
  1604. /* Close the input file that is now open.  */
  1605.  
  1606. void
  1607. file_close ()
  1608. {
  1609.   close (input_desc);
  1610.   input_desc = 0;
  1611.   input_file = 0;
  1612. }
  1613.  
  1614. /* Open the input file specified by 'entry', and return a descriptor.
  1615.    The open file is remembered; if the same file is opened twice in a row,
  1616.    a new open is not actually done.  */
  1617.  
  1618. int
  1619. file_open (entry)
  1620.      register struct file_entry *entry;
  1621. {
  1622.   register int desc;
  1623.  
  1624.   if (entry->superfile)
  1625.     return file_open (entry->superfile);
  1626.  
  1627.   if (entry == input_file)
  1628.     return input_desc;
  1629.  
  1630.   if (input_file) file_close ();
  1631.  
  1632.   if (entry->search_dirs_flag)
  1633.     {
  1634.       register char **p = search_dirs;
  1635.       int i;
  1636.  
  1637.       for (i = 0; i < n_search_dirs; i++)
  1638.     {
  1639.       register char *string
  1640.         = concat (search_dirs[i], "/", entry->filename);
  1641.       desc = open (string, O_RDONLY, 0);
  1642.       if (desc > 0)
  1643.         {
  1644.           entry->filename = string;
  1645.           entry->search_dirs_flag = 0;
  1646.           break;
  1647.         }
  1648.       free (string);
  1649.     }
  1650.     }
  1651.   else
  1652.     desc = open (entry->filename, O_RDONLY, 0);
  1653.  
  1654.   if (desc > 0)
  1655.     {
  1656.       input_file = entry;
  1657.       input_desc = desc;
  1658.       return desc;
  1659.     }
  1660.  
  1661.   perror_file (entry);
  1662.   /* NOTREACHED */
  1663. }
  1664.  
  1665. /* Print the filename of ENTRY on OUTFILE (a stdio stream),
  1666.    and then a newline.  */
  1667.  
  1668. void
  1669. prline_file_name (entry, outfile)
  1670.      struct file_entry *entry;
  1671.      FILE *outfile;
  1672. {
  1673.   print_file_name (entry, outfile);
  1674.   fprintf (outfile, "\n");
  1675. }
  1676.  
  1677. /* Print the filename of ENTRY on OUTFILE (a stdio stream).  */
  1678.  
  1679. void
  1680. print_file_name (entry, outfile)
  1681.      struct file_entry *entry;
  1682.      FILE *outfile;
  1683. {
  1684.   if (entry->superfile)
  1685.     {
  1686.       print_file_name (entry->superfile, outfile);
  1687.       fprintf (outfile, "(%s)", entry->filename);
  1688.     }
  1689.   else
  1690.     fprintf (outfile, "%s", entry->filename);
  1691. }
  1692.  
  1693. /* Return the filename of entry as a string (malloc'd for the purpose) */
  1694.  
  1695. char *
  1696. get_file_name (entry)
  1697.      struct file_entry *entry;
  1698. {
  1699.   char *result, *supfile;
  1700.   if (entry->superfile)
  1701.     {
  1702.       supfile = get_file_name (entry->superfile);
  1703.       result = (char *) xmalloc (strlen (supfile)
  1704.                  + strlen (entry->filename) + 3);
  1705.       sprintf (result, "%s(%s)", supfile, entry->filename);
  1706.       free (supfile);
  1707.     }
  1708.   else
  1709.     {
  1710.       result = (char *) xmalloc (strlen (entry->filename) + 1);
  1711.       strcpy (result, entry->filename);
  1712.     }
  1713.   return result;
  1714. }
  1715.  
  1716. /* Medium-level input routines for rel files.  */
  1717.  
  1718. /* Read a file's header into the proper place in the file_entry.
  1719.    DESC is the descriptor on which the file is open.
  1720.    ENTRY is the file's entry.  */
  1721.  
  1722. void
  1723. read_header (desc, entry)
  1724.      int desc;
  1725.      register struct file_entry *entry;
  1726. {
  1727.   register int len;
  1728.   struct exec *loc = (struct exec *) &entry->header;
  1729.  
  1730.   lseek (desc, entry->starting_offset, 0);
  1731.   len = read (desc, loc, sizeof (struct exec));
  1732.   if (len != sizeof (struct exec))
  1733.     fatal_with_file ("failure reading header of ", entry);
  1734. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  1735.   fix_exec_header_byte_order(loc);
  1736. #endif
  1737.   if (N_BADMAG (*loc))
  1738.     fatal_with_file ("bad magic number in ", entry);
  1739.  
  1740.   entry->header_read_flag = 1;
  1741. }
  1742.  
  1743. /* Read the symbols of file ENTRY into core.
  1744.    Assume it is already open, on descriptor DESC.
  1745.    Also read the length of the string table, which follows the symbol table,
  1746.    but don't read the contents of the string table.  */
  1747.  
  1748. void
  1749. read_entry_symbols (desc, entry)
  1750.      struct file_entry *entry;
  1751.      int desc;
  1752. {
  1753.   int str_size;
  1754.  
  1755.   if (!entry->header_read_flag)
  1756.     read_header (desc, entry);
  1757.  
  1758.   entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms);
  1759.  
  1760.   lseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
  1761.   if (entry->header.a_syms != read (desc, entry->symbols, entry->header.a_syms))
  1762.     fatal_with_file ("premature end of file in symbols of ", entry);
  1763. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  1764.   fix_symbol_byte_order(entry->symbols, entry->header.a_syms);
  1765. #endif
  1766.   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1767.   if (sizeof str_size != read (desc, &str_size, sizeof str_size))
  1768.     fatal_with_file ("bad string table size in ", entry);
  1769. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  1770.   fix_byte_order(&str_size, sizeof(str_size));
  1771. #endif
  1772.   entry->string_size = str_size;
  1773. }
  1774.  
  1775. /* Read the string table of file ENTRY into core.
  1776.    Assume it is already open, on descriptor DESC.
  1777.    Also record whether a GDB symbol segment follows the string table.  */
  1778.  
  1779. void
  1780. read_entry_strings (desc, entry)
  1781.      struct file_entry *entry;
  1782.      int desc;
  1783. {
  1784.   int buffer;
  1785.  
  1786.   if (!entry->header_read_flag)
  1787.     read_header (desc, entry);
  1788.  
  1789.   lseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  1790.   if (entry->string_size != read (desc, entry->strings, entry->string_size))
  1791.     fatal_with_file ("premature end of file in strings of ", entry);
  1792.  
  1793.   /* While we are here, see if the file has a symbol segment at the end.
  1794.      For a separate file, just try reading some more.
  1795.      For a library member, compare current pos against total size.  */
  1796.   if (entry->superfile)
  1797.     {
  1798.       if (entry->total_size == N_STROFF (entry->header) + entry->string_size)
  1799.     return;
  1800.     }
  1801.   else
  1802.     {
  1803.       buffer = read (desc, &buffer, sizeof buffer);
  1804.       if (buffer == 0)
  1805.     return;
  1806.       if (buffer != sizeof buffer)
  1807.     fatal_with_file ("premature end of file in GDB symbol segment of ", entry);
  1808.     }
  1809.  
  1810.   entry->symseg_offset = N_STROFF (entry->header) + entry->string_size;
  1811. }
  1812.  
  1813. /* Read in the symbols of all input files.  */
  1814.  
  1815. void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
  1816. void enter_file_symbols (), enter_global_ref (), search_library ();
  1817.  
  1818. void
  1819. load_symbols ()
  1820. {
  1821.   register int i;
  1822.  
  1823.   if (trace_files) fprintf (stderr, "Loading symbols:\n\n");
  1824.  
  1825.   for (i = 0; i < number_of_files; i++)
  1826.     {
  1827.       register struct file_entry *entry = &file_table[i];
  1828.       read_file_symbols (entry);
  1829.     }
  1830.  
  1831.   if (trace_files) fprintf (stderr, "\n");
  1832. }
  1833.  
  1834. /* If ENTRY is a rel file, read its symbol and string sections into core.
  1835.    If it is a library, search it and load the appropriate members
  1836.    (which means calling this function recursively on those members).  */
  1837.  
  1838. void
  1839. read_file_symbols (entry)
  1840.      register struct file_entry *entry;
  1841. {
  1842.   register int desc;
  1843.   register int len;
  1844.   struct exec hdr;
  1845.  
  1846.   desc = file_open (entry);
  1847.  
  1848.   len = read (desc, &hdr, sizeof hdr);
  1849.   if (len != sizeof hdr)
  1850.     fatal_with_file ("failure reading header of ", entry);
  1851. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  1852.   fix_exec_header_byte_order(&hdr);
  1853. #endif
  1854.   if (!N_BADMAG (hdr))
  1855.     {
  1856.       read_entry_symbols (desc, entry);
  1857.       entry->strings = (char *) alloca (entry->string_size);
  1858.       read_entry_strings (desc, entry);
  1859.       enter_file_symbols (entry);
  1860.       entry->strings = 0;
  1861.     }
  1862.   else
  1863.     {
  1864.       char armag[SARMAG];
  1865.  
  1866.       lseek (desc, 0, 0);
  1867.       if (SARMAG != read (desc, armag, SARMAG) || strncmp (armag, ARMAG, SARMAG))
  1868.     fatal_with_file ("malformed input file (not rel or archive) ", entry);
  1869.       entry->library_flag = 1;
  1870.       search_library (desc, entry);
  1871.     }
  1872.  
  1873.   file_close ();
  1874. }
  1875.  
  1876. /* Enter the external symbol defs and refs of ENTRY in the hash table.  */
  1877.  
  1878. void
  1879. enter_file_symbols (entry)
  1880.      struct file_entry *entry;
  1881. {
  1882.   register struct nlist
  1883.     *p,
  1884.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  1885.   int lowest_set_vector = -1;
  1886.  
  1887.   if (trace_files) prline_file_name (entry, stderr);
  1888.  
  1889.   for (p = entry->symbols; p < end; p++)
  1890.     {
  1891. #ifdef foobar    
  1892.       if (p->n_type == (N_SETV | N_EXT)) continue;
  1893.       if (set_element_prefixes
  1894.       && set_element_prefixed_p (p->n_un.n_strx + entry->strings))
  1895.     p->n_type += (N_SETA - N_ABS);
  1896. #endif
  1897.       if (SET_ELEMENT_P (p->n_type))
  1898.     {
  1899.       set_symbol_count++;
  1900.       if (!relocatable_output)
  1901.         enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1902.     }
  1903.       else if (p->n_type == N_WARNING)
  1904.     {
  1905.       char *name = p->n_un.n_strx + entry->strings;
  1906. #ifndef foobar      
  1907. abort();
  1908. #endif
  1909.       /* Grab the next entry.  */
  1910.       p++;
  1911.       if (p->n_type != (N_UNDF | N_EXT))
  1912.         {
  1913.           fprintf (stderr, "%s: Warning symbol found in %s without external reference following.\n",
  1914.                progname, entry->filename);
  1915.           make_executable = 0;
  1916.           p--;        /* Process normally.  */
  1917.         }
  1918.       else
  1919.         {
  1920.           symbol *sp;
  1921.           char *sname = p->n_un.n_strx + entry->strings; 
  1922.           /* Deal with the warning symbol.  */
  1923.           enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1924.           sp = getsym (sname);
  1925.           sp->warning = (char *) xmalloc (strlen(name) + 1);
  1926.           strcpy (sp->warning, name);
  1927.           warning_count++;
  1928.         }
  1929.     }
  1930.       else if (p->n_type & N_EXT)
  1931.     enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  1932.       else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  1933.     {
  1934.       if ((p->n_un.n_strx + entry->strings)[0] != LPREFIX)
  1935.         non_L_local_sym_count++;
  1936.       local_sym_count++;
  1937.     }
  1938.       else debugger_sym_count++;
  1939.     }
  1940.  
  1941.    /* Count one for the local symbol that we generate,
  1942.       whose name is the file's name (usually) and whose address
  1943.       is the start of the file's text.  */
  1944.  
  1945.   local_sym_count++;
  1946.   non_L_local_sym_count++;
  1947. }
  1948.  
  1949. /* Enter one global symbol in the hash table.
  1950.    NLIST_P points to the `struct nlist' read from the file
  1951.    that describes the global symbol.  NAME is the symbol's name.
  1952.    ENTRY is the file entry for the file the symbol comes from.
  1953.  
  1954.    The `struct nlist' is modified by placing it on a chain of
  1955.    all such structs that refer to the same global symbol.
  1956.    This chain starts in the `refs' field of the symbol table entry
  1957.    and is chained through the `n_name'.  */
  1958.  
  1959. void
  1960. enter_global_ref (nlist_p, name, entry)
  1961.      register struct nlist *nlist_p;
  1962.      char *name;
  1963.      struct file_entry *entry;
  1964. {
  1965.   register symbol *sp = getsym (name);
  1966.   register int type = nlist_p->n_type;
  1967.   int oldref = sp->referenced;
  1968.   int olddef = sp->defined;
  1969.  
  1970.   nlist_p->n_un.n_name = (char *) sp->refs;
  1971.   sp->refs = nlist_p;
  1972.  
  1973.   sp->referenced = 1;
  1974.   if (type != (N_UNDF | N_EXT) || nlist_p->n_value)
  1975.     {
  1976.       if (!sp->defined || sp->defined == (N_UNDF | N_EXT))
  1977.     sp->defined = type;
  1978.  
  1979.       if (oldref && !olddef)
  1980.     /* It used to be undefined and we're defining it.  */
  1981.     undefined_global_sym_count--;
  1982.  
  1983.       if (!olddef && type == (N_UNDF | N_EXT) && nlist_p->n_value)
  1984.     {
  1985.       /* First definition and it's common.  */
  1986.       common_defined_global_count++;
  1987.       sp->max_common_size = nlist_p->n_value;
  1988.     }
  1989.       else if (olddef && sp->max_common_size && type != (N_UNDF | N_EXT))
  1990.     {
  1991.       /* It used to be common and we're defining it as
  1992.          something else.  */
  1993.       common_defined_global_count--;
  1994.       sp->max_common_size = 0;
  1995.     }
  1996.       else if (olddef && sp->max_common_size && type == (N_UNDF | N_EXT)
  1997.       && sp->max_common_size < nlist_p->n_value)
  1998.     /* It used to be common and this is a new common entry to
  1999.        which we need to pay attention.  */
  2000.     sp->max_common_size = nlist_p->n_value;
  2001.  
  2002.       /* Are we defining it as a set element?  */
  2003.       if (SET_ELEMENT_P (type)
  2004.       && (!olddef || (olddef && sp->max_common_size)))
  2005.     set_vector_count++;
  2006.       /* As an indirection?  */
  2007.       else if (type == (N_INDR | N_EXT))
  2008.     {
  2009.       /* Indirect symbols value should be modified to point
  2010.          a symbol being equivalenced to. */
  2011.       nlist_p->n_value
  2012.         = (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
  2013.                      + entry->strings);
  2014.       if ((symbol *) nlist_p->n_value == sp)
  2015.         {
  2016.           /* Somebody redefined a symbol to be itself.  */
  2017.           fprintf (stderr, "%s: Symbol %s indirected to itself.\n",
  2018.                entry->filename, name);
  2019.           /* Rewrite this symbol as being a global text symbol
  2020.          with value 0.  */
  2021.           nlist_p->n_type = sp->defined = N_TEXT | N_EXT;
  2022.           nlist_p->n_value = 0;
  2023.           /* Don't make the output executable.  */
  2024.           make_executable = 0;
  2025.         }
  2026.       else
  2027.         global_indirect_count++;
  2028.     }
  2029.     }
  2030.   else
  2031.     if (!oldref)
  2032. #ifndef DOLLAR_KLUDGE
  2033.       undefined_global_sym_count++;
  2034. #else
  2035.       {
  2036.     if (entry->superfile && type == (N_UNDF | N_EXT) && name[1] == '$')
  2037.       {
  2038.         /* This is an (ISI?) $-conditional; skip it */
  2039.         sp->referenced = 0;
  2040.         if (sp->trace)
  2041.           {
  2042.         fprintf (stderr, "symbol %s is a $-conditional ignored in ", sp->name);
  2043.         print_file_name (entry, stderr);
  2044.         fprintf (stderr, "\n");
  2045.           }
  2046.         return;
  2047.       }
  2048.     else
  2049.       undefined_global_sym_count++;
  2050.       }
  2051. #endif
  2052.  
  2053.   if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified)
  2054.     text_start = nlist_p->n_value;
  2055.  
  2056.   if (sp->trace)
  2057.     {
  2058.       register char *reftype;
  2059.       switch (type & N_TYPE)
  2060.     {
  2061.     case N_UNDF:
  2062.       if (nlist_p->n_value)
  2063.         reftype = "defined as common";
  2064.       else reftype = "referenced";
  2065.       break;
  2066.  
  2067.     case N_ABS:
  2068.       reftype = "defined as absolute";
  2069.       break;
  2070.  
  2071.     case N_TEXT:
  2072.       reftype = "defined in text section";
  2073.       break;
  2074.  
  2075.     case N_DATA:
  2076.       reftype = "defined in data section";
  2077.       break;
  2078.  
  2079.     case N_BSS:
  2080.       reftype = "defined in BSS section";
  2081.       break;
  2082.  
  2083.     case N_SETT:
  2084.       reftype = "is a text set element";
  2085.       break;
  2086.  
  2087.     case N_SETD:
  2088.       reftype = "is a data set element";
  2089.       break;
  2090.  
  2091.     case N_SETB:
  2092.       reftype = "is a BSS set element";
  2093.       break;
  2094.  
  2095.     case N_SETA:
  2096.       reftype = "is an absolute set element";
  2097.       break;
  2098.  
  2099.     case N_SETV:
  2100.       reftype = "defined in data section as vector";
  2101.       break;
  2102.  
  2103.     case N_INDR:
  2104.       reftype = (char *) alloca (23
  2105.                      + strlen ((nlist_p + 1)->n_un.n_strx
  2106.                            + entry->strings));
  2107.       sprintf (reftype, "defined equivalent to %s",
  2108.            (nlist_p + 1)->n_un.n_strx + entry->strings);
  2109.       break;
  2110.  
  2111. #if TARGET_MACHINE==TARGET_SEQUENT
  2112.     case N_SHUNDF:
  2113.       reftype = "shared undf";
  2114.       break;
  2115.  
  2116. /* These conflict with cases above.
  2117.     case N_SHDATA:
  2118.       reftype = "shared data";
  2119.       break;
  2120.  
  2121.     case N_SHBSS:
  2122.       reftype = "shared BSS";
  2123.       break;
  2124. */
  2125.     default:
  2126.       reftype = "I don't know this type";
  2127.       break;
  2128. #endif
  2129.     }
  2130.  
  2131.       fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
  2132.       print_file_name (entry, stderr);
  2133.       fprintf (stderr, "\n");
  2134.     }
  2135. }
  2136.  
  2137. /* This return 0 if the given file entry's symbol table does *not*
  2138.    contain the nlist point entry, and it returns the files entry
  2139.    pointer (cast to unsigned long) if it does. */
  2140.  
  2141. unsigned long
  2142. contains_symbol (entry, n_ptr)
  2143.      struct file_entry *entry;
  2144.      register struct nlist *n_ptr;
  2145. {
  2146.   if (n_ptr >= entry->symbols &&
  2147.       n_ptr < (entry->symbols
  2148.            + (entry->header.a_syms / sizeof (struct nlist))))
  2149.     return (unsigned long) entry;
  2150.   return 0;
  2151. }
  2152.  
  2153.  
  2154. /* Searching libraries */
  2155.  
  2156. struct file_entry *decode_library_subfile ();
  2157. void linear_library (), symdef_library ();
  2158.  
  2159. /* Search the library ENTRY, already open on descriptor DESC.
  2160.    This means deciding which library members to load,
  2161.    making a chain of `struct file_entry' for those members,
  2162.    and entering their global symbols in the hash table.  */
  2163.  
  2164. void
  2165. search_library (desc, entry)
  2166.      int desc;
  2167.      struct file_entry *entry;
  2168. {
  2169.   int member_length;
  2170.   register char *name;
  2171.   register struct file_entry *subentry;
  2172.  
  2173.   if (!undefined_global_sym_count) return;
  2174.  
  2175.   /* Examine its first member, which starts SARMAG bytes in.  */
  2176.   subentry = decode_library_subfile (desc, entry, SARMAG, &member_length);
  2177.   if (!subentry) return;
  2178.  
  2179.   name = subentry->filename;
  2180.   free (subentry);
  2181.  
  2182.   /* Search via __.SYMDEF if that exists, else linearly.  */
  2183.  
  2184.   if (!strcmp (name, "__.SYMDEF"))
  2185.     symdef_library (desc, entry, member_length);
  2186.   else
  2187.     linear_library (desc, entry);
  2188. }
  2189.  
  2190. /* Construct and return a file_entry for a library member.
  2191.    The library's file_entry is library_entry, and the library is open on DESC.
  2192.    SUBFILE_OFFSET is the byte index in the library of this member's header.
  2193.    We store the length of the member into *LENGTH_LOC.  */
  2194.  
  2195. struct file_entry *
  2196. decode_library_subfile (desc, library_entry, subfile_offset, length_loc)
  2197.      int desc;
  2198.      struct file_entry *library_entry;
  2199.      int subfile_offset;
  2200.      int *length_loc;
  2201. {
  2202.   int bytes_read;
  2203.   register int namelen;
  2204.   int member_length;
  2205.   register char *name;
  2206.   struct ar_hdr hdr1;
  2207.   register struct file_entry *subentry;
  2208.  
  2209.   lseek (desc, subfile_offset, 0);
  2210.  
  2211.   bytes_read = read (desc, &hdr1, sizeof hdr1);
  2212.   if (!bytes_read)
  2213.     return 0;        /* end of archive */
  2214.  
  2215.   if (sizeof hdr1 != bytes_read)
  2216.     fatal_with_file ("malformed library archive ", library_entry);
  2217.  
  2218.   if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  2219.     fatal_with_file ("malformatted header of archive member in ", library_entry);
  2220.  
  2221.   subentry = (struct file_entry *) xmalloc (sizeof (struct file_entry));
  2222.   bzero (subentry, sizeof (struct file_entry));
  2223.  
  2224.   for (namelen = 0;
  2225.        namelen < sizeof hdr1.ar_name
  2226.        && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' '
  2227.        && hdr1.ar_name[namelen] != '/';
  2228.        namelen++);
  2229.  
  2230.   name = (char *) xmalloc (namelen+1);
  2231.   strncpy (name, hdr1.ar_name, namelen);
  2232.   name[namelen] = 0;
  2233.  
  2234.   subentry->filename = name;
  2235.   subentry->local_sym_name = name;
  2236.   subentry->symbols = 0;
  2237.   subentry->strings = 0;
  2238.   subentry->subfiles = 0;
  2239.   subentry->starting_offset = subfile_offset + sizeof hdr1;
  2240.   subentry->superfile = library_entry;
  2241.   subentry->library_flag = 0;
  2242.   subentry->header_read_flag = 0;
  2243.   subentry->just_syms_flag = 0;
  2244.   subentry->chain = 0;
  2245.   subentry->total_size = member_length;
  2246.  
  2247.   (*length_loc) = member_length;
  2248.  
  2249.   return subentry;
  2250. }
  2251.  
  2252. int subfile_wanted_p ();
  2253.  
  2254. /* Search a library that has a __.SYMDEF member.
  2255.    DESC is a descriptor on which the library is open.
  2256.      The file pointer is assumed to point at the __.SYMDEF data.
  2257.    ENTRY is the library's file_entry.
  2258.    MEMBER_LENGTH is the length of the __.SYMDEF data.  */
  2259.  
  2260. void
  2261. symdef_library (desc, entry, member_length)
  2262.      int desc;
  2263.      struct file_entry *entry;
  2264.      int member_length;
  2265. {
  2266.   int *symdef_data = (int *) xmalloc (member_length);
  2267.   register struct symdef *symdef_base;
  2268.   char *sym_name_base;
  2269.   int number_of_symdefs;
  2270.   int length_of_strings;
  2271.   int not_finished;
  2272.   int bytes_read;
  2273.   register int i;
  2274.   struct file_entry *prev = 0;
  2275.   int prev_offset = 0;
  2276.  
  2277.   bytes_read = read (desc, symdef_data, member_length);
  2278.   if (bytes_read != member_length)
  2279.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2280. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  2281.   fix_byte_order(symdef_data, sizeof(*symdef_data));
  2282. #endif
  2283.   number_of_symdefs = *symdef_data / sizeof (struct symdef);
  2284.   if (number_of_symdefs < 0 ||
  2285.        number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) > member_length)
  2286.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2287.  
  2288.   symdef_base = (struct symdef *) (symdef_data + 1);
  2289. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  2290.   for (i = 0; i < number_of_symdefs; ++i)
  2291.     {
  2292.       fix_byte_order(&symdef_base[i].symbol_name_string_index, sizeof(int));
  2293.       fix_byte_order(&symdef_base[i].library_member_offset, sizeof(int));
  2294.     }
  2295.   fix_byte_order(symdef_base + number_of_symdefs, sizeof(int));
  2296. #endif
  2297.   length_of_strings = *(int *) (symdef_base + number_of_symdefs);
  2298.  
  2299.   if (length_of_strings < 0
  2300.       || number_of_symdefs * sizeof (struct symdef) + length_of_strings
  2301.       + 2 * sizeof (int) != member_length)
  2302.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2303.  
  2304.   sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs);
  2305.  
  2306.   /* Check all the string indexes for validity.  */
  2307.  
  2308.   for (i = 0; i < number_of_symdefs; i++)
  2309.     {
  2310.       register int index = symdef_base[i].symbol_name_string_index;
  2311.       if (index < 0 || index >= length_of_strings
  2312.       || (index && *(sym_name_base + index - 1)))
  2313.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2314.     }
  2315.  
  2316.   /* Search the symdef data for members to load.
  2317.      Do this until one whole pass finds nothing to load.  */
  2318.  
  2319.   not_finished = 1;
  2320.   while (not_finished)
  2321.     {
  2322.       not_finished = 0;
  2323.  
  2324.       /* Scan all the symbols mentioned in the symdef for ones that we need.
  2325.      Load the library members that contain such symbols.  */
  2326.  
  2327.       for (i = 0;
  2328.        (i < number_of_symdefs
  2329.         && (undefined_global_sym_count || common_defined_global_count));
  2330.        i++)
  2331.     if (symdef_base[i].symbol_name_string_index >= 0)
  2332.       {
  2333.         register symbol *sp;
  2334.  
  2335.         sp = getsym_soft (sym_name_base
  2336.                   + symdef_base[i].symbol_name_string_index);
  2337.  
  2338.         /* If we find a symbol that appears to be needed, think carefully
  2339.            about the archive member that the symbol is in.  */
  2340.  
  2341.         if (sp && ((sp->referenced && !sp->defined)
  2342.                || (sp->defined && sp->max_common_size)))
  2343.           {
  2344.         int junk;
  2345.         register int j;
  2346.         register int offset = symdef_base[i].library_member_offset;
  2347.         struct file_entry *subentry;
  2348.  
  2349.         /* Don't think carefully about any archive member
  2350.            more than once in a given pass.  */
  2351.  
  2352.         if (prev_offset == offset)
  2353.           continue;
  2354.         prev_offset = offset;
  2355.  
  2356.         /* Read the symbol table of the archive member.  */
  2357.  
  2358.         subentry = decode_library_subfile (desc, entry, offset, &junk);
  2359.         if (subentry == 0)
  2360.           fatal ("invalid offset for %s in symbol table of %s",
  2361.              sym_name_base
  2362.              + symdef_base[i].symbol_name_string_index,
  2363.              entry->filename);
  2364.         read_entry_symbols (desc, subentry);
  2365.         subentry->strings = (char *) malloc (subentry->string_size);
  2366.         read_entry_strings (desc, subentry);
  2367.  
  2368.         /* Now scan the symbol table and decide whether to load.  */
  2369.  
  2370.         if (!subfile_wanted_p (subentry))
  2371.           {
  2372.             free (subentry->symbols);
  2373.             free (subentry);
  2374.           }
  2375.         else
  2376.           {
  2377.             /* This member is needed; load it.
  2378.                Since we are loading something on this pass,
  2379.                we must make another pass through the symdef data.  */
  2380.  
  2381.             not_finished = 1;
  2382.  
  2383.             enter_file_symbols (subentry);
  2384.  
  2385.             if (prev)
  2386.               prev->chain = subentry;
  2387.             else entry->subfiles = subentry;
  2388.             prev = subentry;
  2389.  
  2390.             /* Clear out this member's symbols from the symdef data
  2391.                so that following passes won't waste time on them.  */
  2392.  
  2393.             for (j = 0; j < number_of_symdefs; j++)
  2394.               {
  2395.             if (symdef_base[j].library_member_offset == offset)
  2396.               symdef_base[j].symbol_name_string_index = -1;
  2397.               }
  2398.           }
  2399.  
  2400.         /* We'll read the strings again if we need them again.  */
  2401.         free (subentry->strings);
  2402.         subentry->strings = 0;
  2403.           }
  2404.       }
  2405.     }
  2406.  
  2407.   free (symdef_data);
  2408. }
  2409.  
  2410.  
  2411. /* Handle a subentry for a file with no __.SYMDEF. */
  2412.  
  2413. process_subentry (desc, subentry, entry, prev_addr)
  2414.      int desc;
  2415.      register struct file_entry *subentry;
  2416.      struct file_entry **prev_addr, *entry;
  2417. {
  2418.   register struct file_entry *prev = *prev_addr;
  2419.  
  2420.   read_entry_symbols (desc, subentry);
  2421.   subentry->strings = (char *) alloca (subentry->string_size);
  2422.   read_entry_strings (desc, subentry);
  2423.  
  2424.   if (!subfile_wanted_p (subentry))
  2425.     {
  2426.       free (subentry->symbols);
  2427.       free (subentry);
  2428.     }
  2429.   else
  2430.     {
  2431.       enter_file_symbols (subentry);
  2432.  
  2433.       if (prev)
  2434.     prev->chain = subentry;
  2435.       else
  2436.     entry->subfiles = subentry;
  2437.       prev = subentry;
  2438.       subentry->strings = 0; /* Since space will dissapear on return */
  2439.     }
  2440. }
  2441.  
  2442. /* Search a library that has no __.SYMDEF.
  2443.    ENTRY is the library's file_entry.
  2444.    DESC is the descriptor it is open on.  */
  2445.  
  2446. void
  2447. linear_library (desc, entry)
  2448.      int desc;
  2449.      struct file_entry *entry;
  2450. {
  2451.   struct file_entry *prev = 0;
  2452.   register int this_subfile_offset = SARMAG;
  2453.  
  2454.   while (undefined_global_sym_count || common_defined_global_count)
  2455.     {
  2456.       int member_length;
  2457.       register struct file_entry *subentry;
  2458.  
  2459.       subentry = decode_library_subfile (desc, entry, this_subfile_offset,
  2460.                      &member_length);
  2461.       if (!subentry) return;
  2462.  
  2463.       process_subentry (desc, subentry, entry, &prev);
  2464.       this_subfile_offset += member_length + sizeof (struct ar_hdr);
  2465.       if (this_subfile_offset & 1) this_subfile_offset++;
  2466.     }
  2467. }
  2468.  
  2469. /* ENTRY is an entry for a library member.
  2470.    Its symbols have been read into core, but not entered.
  2471.    Return nonzero if we ought to load this member.  */
  2472.  
  2473. int
  2474. subfile_wanted_p (entry)
  2475.      struct file_entry *entry;
  2476. {
  2477.   register struct nlist *p;
  2478.   register struct nlist *end
  2479.     = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2480. #ifdef DOLLAR_KLUDGE
  2481.   register int dollar_cond = 0;
  2482. #endif
  2483.  
  2484.   for (p = entry->symbols; p < end; p++)
  2485.     {
  2486.       register int type = p->n_type;
  2487.       register char *name = p->n_un.n_strx + entry->strings;
  2488.  
  2489.       /* If the symbol has an interesting definition, we could
  2490.      potentially want it.  */
  2491.       if (type & N_EXT
  2492.       && (type != (N_UNDF | N_EXT) || p->n_value
  2493.           
  2494. #ifdef DOLLAR_KLUDGE
  2495.            || name[1] == '$'
  2496. #endif
  2497.           )
  2498.       && !SET_ELEMENT_P (type)
  2499.       && !set_element_prefixed_p (name))
  2500.     {
  2501.       register symbol *sp = getsym_soft (name);
  2502.  
  2503. #ifdef DOLLAR_KLUDGE
  2504.       if (name[1] == '$')
  2505.         {
  2506.           sp = getsym_soft (&name[2]);
  2507.           dollar_cond = 1;
  2508.           if (!sp) continue;
  2509.           if (sp->referenced)
  2510.         {
  2511.           if (write_map)
  2512.             {
  2513.               print_file_name (entry, stdout);
  2514.               fprintf (stdout, " needed due to $-conditional %s\n", name);
  2515.             }
  2516.           return 1;
  2517.         }
  2518.           continue;
  2519.         }
  2520. #endif
  2521.  
  2522.       /* If this symbol has not been hashed, we can't be looking for it. */
  2523.  
  2524.       if (!sp) continue;
  2525.  
  2526.       if ((sp->referenced && !sp->defined)
  2527.           || (sp->defined && sp->max_common_size))
  2528.         {
  2529.           /* This is a symbol we are looking for.  It is either
  2530.              not yet defined or defined as a common.  */
  2531. #ifdef DOLLAR_KLUDGE
  2532.           if (dollar_cond) continue;
  2533. #endif
  2534.           if (type == (N_UNDF | N_EXT))
  2535.         {
  2536.           /* Symbol being defined as common.
  2537.              Remember this, but don't load subfile just for this.  */
  2538.  
  2539.           /* If it didn't used to be common, up the count of
  2540.              common symbols.  */
  2541.           if (!sp->max_common_size)
  2542.             common_defined_global_count++;
  2543.  
  2544.           if (sp->max_common_size < p->n_value)
  2545.             sp->max_common_size = p->n_value;
  2546.           if (!sp->defined)
  2547.             undefined_global_sym_count--;
  2548.           sp->defined = 1;
  2549.           continue;
  2550.         }
  2551.  
  2552.           if (write_map)
  2553.         {
  2554.           print_file_name (entry, stdout);
  2555.           fprintf (stdout, " needed due to %s\n", sp->name);
  2556.         }
  2557.           return 1;
  2558.         }
  2559.     }
  2560.     }
  2561.  
  2562.   return 0;
  2563. }
  2564.  
  2565. void consider_file_section_lengths (), relocate_file_addresses ();
  2566.  
  2567. /* Having entered all the global symbols and found the sizes of sections
  2568.    of all files to be linked, make all appropriate deductions from this data.
  2569.  
  2570.    We propagate global symbol values from definitions to references.
  2571.    We compute the layout of the output file and where each input file's
  2572.    contents fit into it.  */
  2573.  
  2574. void
  2575. digest_symbols ()
  2576. {
  2577.   register int i;
  2578.   int setv_fill_count;
  2579.  
  2580.   if (trace_files)
  2581.     fprintf (stderr, "Digesting symbol information:\n\n");
  2582.  
  2583.   /* Compute total size of sections */
  2584.  
  2585.   each_file (consider_file_section_lengths, 0);
  2586.  
  2587.   /* If necessary, pad text section to full page in the file.
  2588.      Include the padding in the text segment size.  */
  2589.  
  2590. #ifdef NMAGIC
  2591.   if (magic == ZMAGIC || magic == NMAGIC)
  2592. #else
  2593.   if (magic == ZMAGIC)
  2594. #endif
  2595.     {
  2596.       int text_end = text_size + N_TXTOFF (outheader);
  2597.       text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end;
  2598.       text_size += text_pad;
  2599.     }
  2600.  
  2601.   outheader.a_text = text_size;
  2602. #if TARGET_MACHINE==TARGET_SEQUENT
  2603.   outheader.a_text += N_ADDRADJ (outheader);
  2604. #endif
  2605.  
  2606.   /* Make the data segment address start in memory on a suitable boundary.  */
  2607.  
  2608.   if (! Tdata_flag_specified)
  2609.     data_start = N_DATADDR (outheader) + text_start - N_TXTADDR (outheader);
  2610.  
  2611.   /* Make sure bss starts out aligned as much as anyone can want.  */
  2612.  
  2613.   data_size = (data_size + sizeof(double) - 1) & ~(sizeof(double)-1);
  2614.  
  2615.   /* Set up the set element vector */
  2616.  
  2617.   if (!relocatable_output)
  2618.     {
  2619.       /* The set sector size is the number of set elements + a word
  2620.          for each symbol for the length word at the beginning of the
  2621.      vector, plus a word for each symbol for a zero at the end of
  2622.      the vector (for incremental linking).  */
  2623.       set_sect_size
  2624.     = (2 * set_symbol_count + set_vector_count) * sizeof (unsigned long);
  2625.       set_sect_start = data_start + data_size;
  2626.       data_size += set_sect_size;
  2627.       set_vectors = (unsigned long *) xmalloc (set_sect_size);
  2628.       setv_fill_count = 0;
  2629.     }
  2630.  
  2631.   /* Compute start addresses of each file's sections and symbols.  */
  2632.  
  2633.   each_full_file (relocate_file_addresses, 0);
  2634.  
  2635.   /* Now, for each symbol, verify that it is defined globally at most once.
  2636.      Put the global value into the symbol entry.
  2637.      Common symbols are allocated here, in the BSS section.
  2638.      Each defined symbol is given a '->defined' field
  2639.       which is the correct N_ code for its definition,
  2640.       except in the case of common symbols with -r.
  2641.      Then make all the references point at the symbol entry
  2642.      instead of being chained together. */
  2643.  
  2644.   defined_global_sym_count = 0;
  2645.  
  2646.   for (i = 0; i < TABSIZE; i++)
  2647.     {
  2648.       register symbol *sp;
  2649.       for (sp = symtab[i]; sp; sp = sp->link)
  2650.     {
  2651.       /* For each symbol */
  2652.       register struct nlist *p, *next;
  2653.       int defs = 0, com = sp->max_common_size, erred = 0;
  2654.       struct nlist *first_definition;
  2655.       for (p = sp->refs; p; p = next)
  2656.         {
  2657.           register int type = p->n_type;
  2658.  
  2659.           if (SET_ELEMENT_P (type))
  2660.         {
  2661.           if (relocatable_output)
  2662.             fatal ("internal: global ref to set element with -r");
  2663.           if (!defs++)
  2664.             {
  2665.               sp->value = set_sect_start
  2666.             + setv_fill_count++ * sizeof (unsigned long);
  2667.               sp->defined = N_SETV | N_EXT;
  2668.               first_definition = p;
  2669.             }
  2670.           else if ((sp->defined & ~N_EXT) != N_SETV)
  2671.             {
  2672.               sp->multiply_defined = 1;
  2673.               multiple_def_count++;
  2674.             }
  2675.           set_vectors[setv_fill_count++] = p->n_value;
  2676.         }
  2677.           else if ((type & N_EXT) && type != (N_UNDF | N_EXT))
  2678.         {
  2679.           /* non-common definition */
  2680.           if (defs++ && sp->value != p->n_value)
  2681.             {
  2682.               sp->multiply_defined = 1;
  2683.               multiple_def_count++;
  2684.             }
  2685.           sp->value = p->n_value;
  2686.           sp->defined = type;
  2687.           first_definition = p;
  2688.         }
  2689.           next = (struct nlist *) p->n_un.n_name;
  2690.           p->n_un.n_name = (char *) sp;
  2691.         }
  2692.       /* Allocate as common if defined as common and not defined for real */
  2693.       if (com && !defs)
  2694.         {
  2695.           if (!relocatable_output || force_common_definition)
  2696.         {
  2697.           int align = sizeof (int);
  2698.  
  2699.           /* Round up to nearest sizeof (int).  I don't know
  2700.              whether this is necessary or not (given that
  2701.              alignment is taken care of later), but it's
  2702.              traditional, so I'll leave it in.  Note that if
  2703.              this size alignment is ever removed, ALIGN above
  2704.              will have to be initialized to 1 instead of
  2705.              sizeof (int).  */
  2706.           
  2707.           com = (com + sizeof (int) - 1) & (- sizeof (int));
  2708.  
  2709.           while (!(com & align))
  2710.             align <<= 1;
  2711.  
  2712.           align = align > MAX_ALIGNMENT ? MAX_ALIGNMENT : align;
  2713.  
  2714.           bss_size = ((((bss_size + data_size + data_start)
  2715.                   + (align - 1)) & (- align))
  2716.                   - data_size - data_start);
  2717.  
  2718.           sp->value = data_start + data_size + bss_size;
  2719.           sp->defined = N_BSS | N_EXT;
  2720.           bss_size += com;
  2721.           if (write_map)
  2722.             printf ("Allocating common %s: %x at %x\n",
  2723.                 sp->name, com, sp->value);
  2724.         }
  2725.           else
  2726.         {
  2727.           sp->defined = 0;
  2728.           undefined_global_sym_count++;
  2729.         }
  2730.         }
  2731.       /* Set length word at front of vector and zero byte at end.
  2732.          Reverse the vector itself to put it in file order.  */
  2733.       if ((sp->defined & ~N_EXT) == N_SETV)
  2734.         {
  2735.           unsigned long length_word_index
  2736.         = (sp->value - set_sect_start) / sizeof (unsigned long);
  2737.           unsigned long i, tmp;
  2738.  
  2739.           set_vectors[length_word_index]
  2740.         = setv_fill_count - 1 - length_word_index;
  2741. #ifdef foobar
  2742.           /* Reverse the vector.  */
  2743.           for (i = 1;
  2744.            i < (setv_fill_count - length_word_index - 1) / 2 + 1;
  2745.            i++)
  2746.         {
  2747.           tmp = set_vectors[length_word_index + i];
  2748.           set_vectors[length_word_index + i]
  2749.             = set_vectors[setv_fill_count - i];
  2750.           set_vectors[setv_fill_count - i] = tmp;
  2751. #endif        }
  2752.  
  2753.           set_vectors[setv_fill_count++] = 0;
  2754.         }
  2755.       if (sp->defined)
  2756.         defined_global_sym_count++;
  2757.     }
  2758.     }
  2759.  
  2760. #ifdef foobar
  2761.   /* Make sure end of bss is aligned as much as anyone can want.  */
  2762.  
  2763.   bss_size = (bss_size + sizeof(double) - 1) & ~(sizeof(double)-1);
  2764. #endif
  2765.  
  2766.   if (end_symbol)        /* These are null if -r.  */
  2767.     {
  2768.       etext_symbol->value = text_size + text_start;
  2769.       edata_symbol->value = data_start + data_size;
  2770.       end_symbol->value = data_start + data_size + bss_size;
  2771.     }
  2772.  
  2773.   /* Figure the data_pad now, so that it overlaps with the bss addresses.  */
  2774.  
  2775.   if (specified_data_size && specified_data_size > data_size)
  2776.     data_pad = specified_data_size - data_size;
  2777.  
  2778.   if (magic == ZMAGIC)
  2779.     data_pad = ((data_pad + data_size + page_size - 1) & (- page_size))
  2780.                - data_size;
  2781.  
  2782.   bss_size -= data_pad;
  2783.   if (bss_size < 0) bss_size = 0;
  2784.  
  2785.   data_size += data_pad;
  2786. }
  2787.  
  2788. /* Accumulate the section sizes of input file ENTRY
  2789.    into the section sizes of the output file.  */
  2790.  
  2791. void
  2792. consider_file_section_lengths (entry)
  2793.      register struct file_entry *entry;
  2794. {
  2795.   if (entry->just_syms_flag)
  2796.     return;
  2797.  
  2798.   entry->text_start_address = text_size;
  2799.   /* If there were any vectors, we need to chop them off */
  2800.   text_size += entry->header.a_text;
  2801.   entry->data_start_address = data_size;
  2802.   data_size += entry->header.a_data;
  2803.   entry->bss_start_address = bss_size;
  2804.   bss_size += entry->header.a_bss;
  2805.  
  2806.   text_reloc_size += entry->header.a_trsize;
  2807.   data_reloc_size += entry->header.a_drsize;
  2808. }
  2809.  
  2810. /* Determine where the sections of ENTRY go into the output file,
  2811.    whose total section sizes are already known.
  2812.    Also relocate the addresses of the file's local and debugger symbols.  */
  2813.  
  2814. void
  2815. relocate_file_addresses (entry)
  2816.      register struct file_entry *entry;
  2817. {
  2818.   entry->text_start_address += text_start;
  2819.   /* Note that `data_start' and `data_size' have not yet been
  2820.      adjusted for `data_pad'.  If they had been, we would get the wrong
  2821.      results here.  */
  2822.   entry->data_start_address += data_start;
  2823.   entry->bss_start_address += data_start + data_size;
  2824.  
  2825.   {
  2826.     register struct nlist *p;
  2827.     register struct nlist *end
  2828.       = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2829.  
  2830.     for (p = entry->symbols; p < end; p++)
  2831.       {
  2832.     /* If this belongs to a section, update it by the section's start address */
  2833.     register int type = p->n_type & N_TYPE;
  2834.  
  2835.     switch (type)
  2836.       {
  2837.       case N_TEXT:
  2838.       case N_SETT:
  2839.         p->n_value += entry->text_start_address;
  2840.         break;
  2841.       case N_DATA:
  2842.       case N_SETV:
  2843.       case N_SETD:
  2844.         /* A symbol whose value is in the data section
  2845.            is present in the input file as if the data section
  2846.            started at an address equal to the length of the file's text.  */
  2847.         p->n_value += entry->data_start_address - entry->header.a_text;
  2848.         break;
  2849.       case N_BSS:
  2850.       case N_SETB:
  2851.         /* likewise for symbols with value in BSS.  */
  2852.         p->n_value += entry->bss_start_address
  2853.           - entry->header.a_text - entry->header.a_data;
  2854.         break;
  2855.       }
  2856.       }
  2857.   }
  2858. }
  2859.  
  2860. void describe_file_sections (), list_file_locals ();
  2861.  
  2862. /* Print a complete or partial map of the output file.  */
  2863.  
  2864. void
  2865. print_symbols (outfile)
  2866.      FILE *outfile;
  2867. {
  2868.   register int i;
  2869.  
  2870.   fprintf (outfile, "\nFiles:\n\n");
  2871.  
  2872.   each_file (describe_file_sections, outfile);
  2873.  
  2874.   fprintf (outfile, "\nGlobal symbols:\n\n");
  2875.  
  2876.   for (i = 0; i < TABSIZE; i++)
  2877.     {
  2878.       register symbol *sp;
  2879.       for (sp = symtab[i]; sp; sp = sp->link)
  2880.     {
  2881.       if (sp->defined == 1)
  2882.         fprintf (outfile, "  %s: common, length 0x%x\n", sp->name, sp->max_common_size);
  2883.       if (sp->defined)
  2884.         fprintf (outfile, "  %s: 0x%x\n", sp->name, sp->value);
  2885.       else if (sp->referenced)
  2886.         fprintf (outfile, "  %s: undefined\n", sp->name);
  2887.     }
  2888.     }
  2889.  
  2890.   each_file (list_file_locals, outfile);
  2891. }
  2892.  
  2893. void
  2894. describe_file_sections (entry, outfile)
  2895.      struct file_entry *entry;
  2896.      FILE *outfile;
  2897. {
  2898.   fprintf (outfile, "  ");
  2899.   print_file_name (entry, outfile);
  2900.   if (entry->just_syms_flag)
  2901.     fprintf (outfile, " symbols only\n", 0);
  2902.   else
  2903.     fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n",
  2904.          entry->text_start_address, entry->header.a_text,
  2905.          entry->data_start_address, entry->header.a_data,
  2906.          entry->bss_start_address, entry->header.a_bss);
  2907. }
  2908.  
  2909. void
  2910. list_file_locals (entry, outfile)
  2911.      struct file_entry *entry;
  2912.      FILE *outfile;
  2913. {
  2914.   register struct nlist
  2915.     *p,
  2916.     *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2917.  
  2918.   entry->strings = (char *) alloca (entry->string_size);
  2919.   read_entry_strings (file_open (entry), entry);
  2920.  
  2921.   fprintf (outfile, "\nLocal symbols of ");
  2922.   print_file_name (entry, outfile);
  2923.   fprintf (outfile, ":\n\n");
  2924.  
  2925.   for (p = entry->symbols; p < end; p++)
  2926.     /* If this is a definition,
  2927.        update it if necessary by this file's start address.  */
  2928.     if (!(p->n_type & (N_STAB | N_EXT)))
  2929.       fprintf (outfile, "  %s: 0x%x\n",
  2930.            entry->strings + p->n_un.n_strx, p->n_value);
  2931.  
  2932.   entry->strings = 0;        /* All done with them.  */
  2933. }
  2934.  
  2935.  
  2936. /* Static vars for do_warnings and subroutines of it */
  2937. int list_unresolved_refs;    /* List unresolved refs */
  2938. int list_warning_symbols;    /* List warning syms */
  2939. int list_multiple_defs;        /* List multiple definitions */
  2940.  
  2941. /*
  2942.  * Structure for communication between do_file_warnings and it's
  2943.  * helper routines.  Will in practice be an array of three of these:
  2944.  * 0) Current line, 1) Next line, 2) Source file info.
  2945.  */
  2946. struct line_debug_entry
  2947. {
  2948.   int line;
  2949.   char *filename;
  2950.   struct nlist *sym;
  2951. };
  2952.  
  2953. void qsort ();
  2954. /*
  2955.  * Helper routines for do_file_warnings.
  2956.  */
  2957.  
  2958. /* Return an integer less than, equal to, or greater than 0 as per the
  2959.    relation between the two relocation entries.  Used by qsort.  */
  2960.  
  2961. int
  2962. relocation_entries_relation (rel1, rel2)
  2963.      struct relocation_info *rel1, *rel2;
  2964. {
  2965.   return RELOC_ADDRESS(rel1) - RELOC_ADDRESS(rel2);
  2966. }
  2967.  
  2968. /* Moves to the next debugging symbol in the file.  USE_DATA_SYMBOLS
  2969.    determines the type of the debugging symbol to look for (DSLINE or
  2970.    SLINE).  STATE_POINTER keeps track of the old and new locatiosn in
  2971.    the file.  It assumes that state_pointer[1] is valid; ie
  2972.    that it.sym points into some entry in the symbol table.  If
  2973.    state_pointer[1].sym == 0, this routine should not be called.  */
  2974.  
  2975. int
  2976. next_debug_entry (use_data_symbols, state_pointer)
  2977.      register int use_data_symbols;
  2978.      /* Next must be passed by reference! */
  2979.      struct line_debug_entry state_pointer[3];
  2980. {
  2981.   register struct line_debug_entry
  2982.     *current = state_pointer,
  2983.     *next = state_pointer + 1,
  2984.     /* Used to store source file */
  2985.     *source = state_pointer + 2;
  2986.   struct file_entry *entry = (struct file_entry *) source->sym;
  2987.  
  2988.   current->sym = next->sym;
  2989.   current->line = next->line;
  2990.   current->filename = next->filename;
  2991.  
  2992.   while (++(next->sym) < (entry->symbols
  2993.               + entry->header.a_syms/sizeof (struct nlist)))
  2994.     {
  2995.       /* n_type is a char, and N_SOL, N_EINCL and N_BINCL are > 0x80, so
  2996.        * may look negative...therefore, must mask to low bits
  2997.        */
  2998.       switch (next->sym->n_type & 0xff) 
  2999.     {
  3000.     case N_SLINE:
  3001.       if (use_data_symbols) continue;
  3002.       next->line = next->sym->n_desc;
  3003.       return 1;
  3004.     case N_DSLINE:
  3005.       if (!use_data_symbols) continue;
  3006.       next->line = next->sym->n_desc;
  3007.       return 1;
  3008. #ifdef HAVE_SUN_STABS
  3009.     case N_EINCL:
  3010.       next->filename = source->filename;
  3011.       continue;
  3012. #endif
  3013.     case N_SO:
  3014.       source->filename = next->sym->n_un.n_strx + entry->strings;
  3015.       source->line++;
  3016. #ifdef HAVE_SUN_STABS
  3017.     case N_BINCL:
  3018. #endif
  3019.     case N_SOL:
  3020.       next->filename
  3021.         = next->sym->n_un.n_strx + entry->strings;
  3022.     default:
  3023.       continue;
  3024.     }
  3025.     }
  3026.   next->sym = (struct nlist *) 0;
  3027.   return 0;
  3028. }
  3029.  
  3030. /* Create a structure to save the state of a scan through the debug
  3031.    symbols.  USE_DATA_SYMBOLS is set if we should be scanning for
  3032.    DSLINE's instead of SLINE's.  entry is the file entry which points
  3033.    at the symbols to use.  */
  3034.  
  3035. struct line_debug_entry *
  3036. init_debug_scan (use_data_symbols, entry)
  3037.      int use_data_symbols;
  3038.      struct file_entry *entry;
  3039. {
  3040.   struct line_debug_entry
  3041.     *state_pointer
  3042.       = (struct line_debug_entry *)
  3043.     xmalloc (3 * sizeof (struct line_debug_entry));
  3044.   register struct line_debug_entry
  3045.     *current = state_pointer,
  3046.     *next = state_pointer + 1,
  3047.     *source = state_pointer + 2; /* Used to store source file */
  3048.  
  3049.   struct nlist *tmp;
  3050.  
  3051.   for (tmp = entry->symbols;
  3052.        tmp < (entry->symbols
  3053.           + entry->header.a_syms/sizeof (struct nlist));
  3054.        tmp++)
  3055.     if (tmp->n_type == (int) N_SO)
  3056.       break;
  3057.  
  3058.   if (tmp >= (entry->symbols
  3059.           + entry->header.a_syms/sizeof (struct nlist)))
  3060.     {
  3061.       /* I believe this translates to "We lose" */
  3062.       current->filename = next->filename = entry->filename;
  3063.       current->line = next->line = -1;
  3064.       current->sym = next->sym = (struct nlist *) 0;
  3065.       return state_pointer;
  3066.     }
  3067.  
  3068.   next->line = source->line = 0;
  3069.   next->filename = source->filename
  3070.     = (tmp->n_un.n_strx + entry->strings);
  3071.   source->sym = (struct nlist *) entry;
  3072.   next->sym = tmp;
  3073.  
  3074.   next_debug_entry (use_data_symbols, state_pointer); /* To setup next */
  3075.  
  3076.   if (!next->sym)        /* No line numbers for this section; */
  3077.                 /* setup output results as appropriate */
  3078.     {
  3079.       if (source->line)
  3080.     {
  3081.       current->filename = source->filename = entry->filename;
  3082.       current->line = -1;    /* Don't print lineno */
  3083.     }
  3084.       else
  3085.     {
  3086.       current->filename = source->filename;
  3087.       current->line = 0;
  3088.     }
  3089.       return state_pointer;
  3090.     }
  3091.  
  3092.  
  3093.   next_debug_entry (use_data_symbols, state_pointer); /* To setup current */
  3094.  
  3095.   return state_pointer;
  3096. }
  3097.  
  3098. /* Takes an ADDRESS (in either text or data space) and a STATE_POINTER
  3099.    which describes the current location in the implied scan through
  3100.    the debug symbols within the file which ADDRESS is within, and
  3101.    returns the source line number which corresponds to ADDRESS.  */
  3102.   
  3103. int
  3104. address_to_line (address, state_pointer)
  3105.      unsigned long address;
  3106.      /* Next must be passed by reference! */
  3107.      struct line_debug_entry state_pointer[3];
  3108. {
  3109.   struct line_debug_entry
  3110.     *current = state_pointer,
  3111.     *next = state_pointer + 1;
  3112.   struct line_debug_entry *tmp_pointer;
  3113.  
  3114.   int use_data_symbols;
  3115.  
  3116.   if (next->sym)
  3117.     use_data_symbols = (next->sym->n_type & N_TYPE) == N_DATA;
  3118.   else
  3119.     return current->line;
  3120.  
  3121.   /* Go back to the beginning if we've already passed it.  */
  3122.   if (current->sym->n_value > address)
  3123.     {
  3124.       tmp_pointer = init_debug_scan (use_data_symbols,
  3125.                      (struct file_entry *)
  3126.                      ((state_pointer + 2)->sym));
  3127.       state_pointer[0] = tmp_pointer[0];
  3128.       state_pointer[1] = tmp_pointer[1];
  3129.       state_pointer[2] = tmp_pointer[2];
  3130.       free (tmp_pointer);
  3131.     }
  3132.  
  3133.   /* If we're still in a bad way, return -1, meaning invalid line.  */
  3134.   if (current->sym->n_value > address)
  3135.     return -1;
  3136.  
  3137.   while (next->sym
  3138.      && next->sym->n_value <= address
  3139.      && next_debug_entry (use_data_symbols, state_pointer))
  3140.     ;
  3141.   return current->line;
  3142. }
  3143.  
  3144.  
  3145. /* Macros for manipulating bitvectors.  */
  3146. #define    BIT_SET_P(bv, index)    ((bv)[(index) >> 3] & 1 << ((index) & 0x7))
  3147. #define    SET_BIT(bv, index)    ((bv)[(index) >> 3] |= 1 << ((index) & 0x7))
  3148.  
  3149. /* This routine will scan through the relocation data of file ENTRY,
  3150.    printing out references to undefined symbols and references to
  3151.    symbols defined in files with N_WARNING symbols.  If DATA_SEGMENT
  3152.    is non-zero, it will scan the data relocation segment (and use
  3153.    N_DSLINE symbols to track line number); otherwise it will scan the
  3154.    text relocation segment.  Warnings will be printed on the output
  3155.    stream OUTFILE.  Eventually, every nlist symbol mapped through will
  3156.    be marked in the NLIST_BITVECTOR, so we don't repeat ourselves when
  3157.    we scan the nlists themselves.  */
  3158.  
  3159. do_relocation_warnings (entry, data_segment, outfile, nlist_bitvector)
  3160.      struct file_entry *entry;
  3161.      int data_segment;
  3162.      FILE *outfile;
  3163.      unsigned char *nlist_bitvector;
  3164. {
  3165.   struct relocation_info
  3166.     *reloc_start = data_segment ? entry->datarel : entry->textrel,
  3167.     *reloc;
  3168.   int reloc_size
  3169.     = ((data_segment ? entry->header.a_drsize : entry->header.a_trsize)
  3170.        / sizeof (struct relocation_info));
  3171.   int start_of_segment
  3172.     = (data_segment ? entry->data_start_address : entry->text_start_address);
  3173.   struct nlist *start_of_syms = entry->symbols;
  3174.   struct line_debug_entry *state_pointer
  3175.     = init_debug_scan (data_segment != 0, entry);
  3176.   register struct line_debug_entry
  3177.     *current = state_pointer,
  3178.     *next = state_pointer + 1,
  3179.     *source = state_pointer + 2;
  3180.   /* Assigned to generally static values; should not be written into.  */
  3181.   char *errfmt;
  3182.   /* Assigned to alloca'd values cand copied into; should be freed
  3183.      when done.  */
  3184.   char *errmsg;
  3185.   int invalidate_line_number;
  3186.  
  3187.   /* We need to sort the relocation info here.  Sheesh, so much effort
  3188.      for one lousy error optimization. */
  3189.  
  3190.   qsort (reloc_start, reloc_size, sizeof (struct relocation_info),
  3191.      relocation_entries_relation);
  3192.  
  3193.   for (reloc = reloc_start;
  3194.        reloc < (reloc_start + reloc_size);
  3195.        reloc++)
  3196.     {
  3197.       register struct nlist *s;
  3198.       register symbol *g;
  3199.  
  3200.       /* If the relocation isn't resolved through a symbol, continue */
  3201.       if (!RELOC_EXTERN_P(reloc))
  3202.     continue;
  3203.  
  3204.       s = &(entry->symbols[RELOC_SYMBOL(reloc)]);
  3205.  
  3206.       /* Local symbols shouldn't ever be used by relocation info, so
  3207.      the next should be safe.
  3208.      This is, of course, wrong.  References to local BSS symbols can be
  3209.      the targets of relocation info, and they can (must) be
  3210.      resolved through symbols.  However, these must be defined properly,
  3211.      (the assembler would have caught it otherwise), so we can
  3212.      ignore these cases.  */
  3213.       if (!(s->n_type & N_EXT))
  3214.     continue;
  3215.  
  3216.       g = (symbol *) s->n_un.n_name;
  3217.       errmsg = 0;
  3218.  
  3219.       if (!g->defined && list_unresolved_refs) /* Reference */
  3220.     {
  3221.       /* Mark as being noted by relocation warning pass.  */
  3222.       SET_BIT (nlist_bitvector, s - start_of_syms);
  3223.       
  3224.       if (g->undef_refs >= MAX_UREFS_PRINTED)    /* Listed too many */
  3225.         continue;
  3226.  
  3227.       /* Undefined symbol which we should mention */
  3228.  
  3229.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3230.         {
  3231.           errfmt = "More undefined symbol %s refs follow";
  3232.           invalidate_line_number = 1;
  3233.         }
  3234.       else
  3235.         {
  3236.           errfmt = "Undefined symbol %s referenced from %s segment";
  3237.           invalidate_line_number = 0;
  3238.         }
  3239.     }
  3240.       else                         /* Defined */
  3241.     {
  3242.       /* Potential symbol warning here */
  3243.       if (!g->warning) continue;
  3244.  
  3245.       /* Mark as being noted by relocation warning pass.  */
  3246.       SET_BIT (nlist_bitvector, s - start_of_syms);
  3247.       
  3248.       errfmt = 0;
  3249.       errmsg = g->warning;
  3250.       invalidate_line_number = 0;
  3251.     }
  3252.       
  3253.  
  3254.       /* If errfmt == 0, errmsg has already been defined.  */
  3255.       if (errfmt != 0)
  3256.     {
  3257.       errmsg = (char *) xmalloc (strlen (errfmt) + strlen (g->name) + 1);
  3258.       sprintf (errmsg, errfmt, g->name, data_segment ? "data" : "text");
  3259.     }
  3260.  
  3261.       address_to_line (RELOC_ADDRESS (reloc) + start_of_segment,
  3262.                state_pointer);
  3263.  
  3264.       if (current->line >=0)
  3265.     fprintf (outfile, "%s:%d: %s\n", current->filename,
  3266.          invalidate_line_number ? 0 : current->line, errmsg);
  3267.       else
  3268.     fprintf (outfile, "%s: %s\n", current->filename, errmsg);
  3269.  
  3270.       if (errfmt != 0)
  3271.     free (errmsg);
  3272.     }
  3273.  
  3274.   free (state_pointer);
  3275. }
  3276.  
  3277. /* Print on OUTFILE a list of all warnings generated by references
  3278.    and/or definitions in the file ENTRY.  List source file and line
  3279.    number if possible, just the .o file if not. */
  3280.  
  3281. void
  3282. do_file_warnings (entry, outfile)
  3283.      struct file_entry *entry;
  3284.      FILE *outfile;
  3285. {
  3286.   int number_of_syms = entry->header.a_syms / sizeof (struct nlist);
  3287.   unsigned char *nlist_bitvector
  3288.     = (unsigned char *) alloca ((number_of_syms >> 3) + 1);
  3289.   struct line_debug_entry *text_scan, *data_scan;
  3290.   int i;
  3291.   char *errfmt, *file_name;
  3292.   int line_number;
  3293.   int dont_allow_symbol_name;
  3294.  
  3295.   bzero (nlist_bitvector, (number_of_syms >> 3) + 1);
  3296.  
  3297.   /* Read in the files strings if they aren't available */
  3298.   if (!entry->strings)
  3299.     {
  3300.       int desc;
  3301.  
  3302.       entry->strings = (char *) alloca (entry->string_size);
  3303.       desc = file_open (entry);
  3304.       read_entry_strings (desc, entry);
  3305.     }
  3306.  
  3307.   read_file_relocation (entry);
  3308.  
  3309.   /* Do text warnings based on a scan through the relocation info.  */
  3310.   do_relocation_warnings (entry, 0, outfile, nlist_bitvector);
  3311.  
  3312.   /* Do data warnings based on a scan through the relocation info.  */
  3313.   do_relocation_warnings (entry, 1, outfile, nlist_bitvector);
  3314.  
  3315.   /* Scan through all of the nlist entries in this file and pick up
  3316.      anything that the scan through the relocation stuff didn't.  */
  3317.  
  3318.   text_scan = init_debug_scan (0, entry);
  3319.   data_scan = init_debug_scan (1, entry);
  3320.  
  3321.   for (i = 0; i < number_of_syms; i++)
  3322.     {
  3323.       struct nlist *s;
  3324.       struct glosym *g;
  3325.  
  3326.       s = entry->symbols + i;
  3327.  
  3328.       if (!(s->n_type & N_EXT))
  3329.     continue;
  3330.  
  3331.       g = (symbol *) s->n_un.n_name;
  3332.       dont_allow_symbol_name = 0;
  3333.  
  3334.       if (list_multiple_defs && g->multiply_defined)
  3335.     {
  3336.       errfmt = "Definition of symbol %s (multiply defined)";
  3337.       switch (s->n_type)
  3338.         {
  3339.         case N_TEXT | N_EXT:
  3340.           line_number = address_to_line (s->n_value, text_scan);
  3341.           file_name = text_scan[0].filename;
  3342.           break;
  3343.         case N_DATA | N_EXT:
  3344.           line_number = address_to_line (s->n_value, data_scan);
  3345.           file_name = data_scan[0].filename;
  3346.           break;
  3347.         case N_SETA | N_EXT:
  3348.         case N_SETT | N_EXT:
  3349.         case N_SETD | N_EXT:
  3350.         case N_SETB | N_EXT:
  3351.           if (g->multiply_defined == 2)
  3352.         continue;
  3353.           errfmt = "First set element definition of symbol %s (multiply defined)";
  3354.           break;
  3355.         default:
  3356.           continue;        /* Don't print out multiple defs
  3357.                    at references.  */
  3358.         }
  3359.     }
  3360.       else if (BIT_SET_P (nlist_bitvector, i))
  3361.     continue;
  3362.       else if (list_unresolved_refs && !g->defined)
  3363.     {
  3364.       if (g->undef_refs >= MAX_UREFS_PRINTED)
  3365.         continue;
  3366.       
  3367.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3368.         errfmt = "More undefined \"%s\" refs follow";
  3369.       else
  3370.         errfmt = "Undefined symbol \"%s\" referenced";
  3371.       line_number = -1;
  3372.     }
  3373.       else if (g->warning)
  3374.     {
  3375.       /* There are two cases in which we don't want to
  3376.          do this.  The first is if this is a definition instead of
  3377.          a reference.  The second is if it's the reference used by
  3378.          the warning stabs itself.  */
  3379.       if (s->n_type != (N_EXT | N_UNDF)
  3380.           || (i && (s-1)->n_type == N_WARNING))
  3381.         continue;
  3382.  
  3383.       errfmt = g->warning;
  3384.       line_number = -1;
  3385.       dont_allow_symbol_name = 1;
  3386.     }
  3387.       else
  3388.     continue;
  3389.       
  3390.       if (line_number == -1)
  3391.     fprintf (outfile, "%s: ", entry->filename);
  3392.       else
  3393.     fprintf (outfile, "%s:%d: ", file_name, line_number);
  3394.  
  3395.       if (dont_allow_symbol_name)
  3396.     fprintf (outfile, "%s", errfmt);
  3397.       else
  3398.     fprintf (outfile, errfmt, g->name);
  3399.  
  3400.       fputc ('\n', outfile);
  3401.     }
  3402.   free (text_scan);
  3403.   free (data_scan);
  3404.   entry->strings = 0;        /* Since it will dissapear anyway.  */
  3405. }
  3406.  
  3407. do_warnings (outfile)
  3408.      FILE *outfile;
  3409. {
  3410.   int i;
  3411.  
  3412.   list_unresolved_refs = !relocatable_output && undefined_global_sym_count;
  3413.   list_warning_symbols = warning_count;
  3414.   list_multiple_defs = multiple_def_count != 0;
  3415.  
  3416.   if (!(list_unresolved_refs ||
  3417.     list_warning_symbols ||
  3418.     list_multiple_defs      ))
  3419.     /* No need to run this routine */
  3420.     return;
  3421.  
  3422.   each_file (do_file_warnings, outfile);
  3423.  
  3424.   if (list_unresolved_refs || list_multiple_defs)
  3425.     make_executable = 0;
  3426. }
  3427.  
  3428. /* Write the output file */
  3429.  
  3430. void
  3431. write_output ()
  3432. {
  3433.   struct stat statbuf;
  3434.   int filemode;
  3435.  
  3436.   outdesc = open (output_filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  3437.   if (outdesc < 0) perror_name (output_filename);
  3438.  
  3439.   if (fstat (outdesc, &statbuf) < 0)
  3440.     perror_name (output_filename);
  3441.  
  3442.   filemode = statbuf.st_mode;
  3443.  
  3444.   chmod (output_filename, filemode & ~0111);
  3445.  
  3446.   /* Output the a.out header.  */
  3447.   write_header ();
  3448.  
  3449.   /* Output the text and data segments, relocating as we go.  */
  3450.   write_text ();
  3451.   write_data ();
  3452.  
  3453.   /* Output the merged relocation info, if requested with `-r'.  */
  3454.   if (relocatable_output)
  3455.     write_rel ();
  3456.  
  3457.   /* Output the symbol table (both globals and locals).  */
  3458.   write_syms ();
  3459.  
  3460.   /* Copy any GDB symbol segments from input files.  */
  3461.   write_symsegs ();
  3462.  
  3463.   close (outdesc);
  3464.  
  3465.   if (chmod (output_filename, filemode | 0111) == -1)
  3466.     perror_name (output_filename);
  3467. }
  3468.  
  3469. void modify_location (), perform_relocation (), copy_text (), copy_data ();
  3470.  
  3471. void
  3472. write_header ()
  3473. {
  3474.   N_SET_MAGIC (outheader, magic);
  3475.   outheader.a_text = text_size;
  3476. #if TARGET_MACHINE==TARGET_SEQUENT
  3477.   outheader.a_text += N_ADDRADJ (outheader);
  3478. #endif
  3479.   outheader.a_data = data_size;
  3480.   outheader.a_bss = bss_size;
  3481.   outheader.a_entry = (entry_symbol ? entry_symbol->value
  3482.                : text_start + entry_offset);
  3483. #ifdef COFF_ENCAPSULATE
  3484.   if (need_coff_header)
  3485.     {
  3486.       /* We are encapsulating BSD format within COFF format.  */
  3487.       struct coffscn *tp, *dp, *bp;
  3488.  
  3489.       tp = &coffheader.scns[0];
  3490.       dp = &coffheader.scns[1];
  3491.       bp = &coffheader.scns[2];
  3492.  
  3493.       strcpy (tp->s_name, ".text");
  3494.       tp->s_paddr = text_start;
  3495.       tp->s_vaddr = text_start;
  3496.       tp->s_size = text_size;
  3497.       tp->s_scnptr = sizeof (struct coffheader) + sizeof (struct exec);
  3498.       tp->s_relptr = 0;
  3499.       tp->s_lnnoptr = 0;
  3500.       tp->s_nreloc = 0;
  3501.       tp->s_nlnno = 0;
  3502.       tp->s_flags = 0x20;
  3503.       strcpy (dp->s_name, ".data");
  3504.       dp->s_paddr = data_start;
  3505.       dp->s_vaddr = data_start;
  3506.       dp->s_size = data_size;
  3507.       dp->s_scnptr = tp->s_scnptr + tp->s_size;
  3508.       dp->s_relptr = 0;
  3509.       dp->s_lnnoptr = 0;
  3510.       dp->s_nreloc = 0;
  3511.       dp->s_nlnno = 0;
  3512.       dp->s_flags = 0x40;
  3513.       strcpy (bp->s_name, ".bss");
  3514.       bp->s_paddr = dp->s_vaddr + dp->s_size;
  3515.       bp->s_vaddr = bp->s_paddr;
  3516.       bp->s_size = bss_size;
  3517.       bp->s_scnptr = 0;
  3518.       bp->s_relptr = 0;
  3519.       bp->s_lnnoptr = 0;
  3520.       bp->s_nreloc = 0;
  3521.       bp->s_nlnno = 0;
  3522.       bp->s_flags = 0x80;
  3523.  
  3524.       coffheader.f_magic = COFF_MAGIC;
  3525.       coffheader.f_nscns = 3;
  3526.       /* store an unlikely time so programs can
  3527.        * tell that there is a bsd header
  3528.        */
  3529.       coffheader.f_timdat = 1;
  3530.       coffheader.f_symptr = 0;
  3531.       coffheader.f_nsyms = 0;
  3532.       coffheader.f_opthdr = 28;
  3533.       coffheader.f_flags = 0x103;
  3534.       /* aouthdr */
  3535.       coffheader.magic = ZMAGIC;
  3536.       coffheader.vstamp = 0;
  3537.       coffheader.tsize = tp->s_size;
  3538.       coffheader.dsize = dp->s_size;
  3539.       coffheader.bsize = bp->s_size;
  3540.       coffheader.entry = outheader.a_entry;
  3541.       coffheader.text_start = tp->s_vaddr;
  3542.       coffheader.data_start = dp->s_vaddr;
  3543.     }
  3544. #endif
  3545.  
  3546. #ifdef INITIALIZE_HEADER
  3547.   INITIALIZE_HEADER;
  3548. #endif
  3549.  
  3550.   if (strip_symbols == STRIP_ALL)
  3551.     nsyms = 0;
  3552.   else
  3553.     {
  3554.       nsyms = (defined_global_sym_count
  3555.            + undefined_global_sym_count);
  3556.       if (discard_locals == DISCARD_L)
  3557.     nsyms += non_L_local_sym_count;
  3558.       else if (discard_locals == DISCARD_NONE)
  3559.     nsyms += local_sym_count;
  3560.       /* One extra for following reference on indirects */
  3561.       if (relocatable_output)
  3562.     nsyms += set_symbol_count + global_indirect_count;
  3563.     }
  3564.  
  3565.   if (strip_symbols == STRIP_NONE)
  3566.     nsyms += debugger_sym_count;
  3567.  
  3568.   outheader.a_syms = nsyms * sizeof (struct nlist);
  3569.  
  3570.   if (relocatable_output)
  3571.     {
  3572.       outheader.a_trsize = text_reloc_size;
  3573.       outheader.a_drsize = data_reloc_size;
  3574.     }
  3575.   else
  3576.     {
  3577.       outheader.a_trsize = 0;
  3578.       outheader.a_drsize = 0;
  3579.     }
  3580.  
  3581. #ifdef COFF_ENCAPSULATE
  3582.   if (need_coff_header)
  3583.     mywrite (&coffheader, sizeof coffheader, 1, outdesc);
  3584. #endif
  3585. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3586.   fix_exec_header_byte_order(&outheader);
  3587. #endif
  3588.   mywrite (&outheader, sizeof (struct exec), 1, outdesc);
  3589. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3590.   fix_exec_header_byte_order(&outheader);
  3591. #endif
  3592.  
  3593.   /* Output whatever padding is required in the executable file
  3594.      between the header and the start of the text.  */
  3595.  
  3596. #ifndef COFF_ENCAPSULATE
  3597.   padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc);
  3598. #endif
  3599. }
  3600.  
  3601. /* Relocate the text segment of each input file
  3602.    and write to the output file.  */
  3603.  
  3604. void
  3605. write_text ()
  3606. {
  3607.   if (trace_files)
  3608.     fprintf (stderr, "Copying and relocating text:\n\n");
  3609.  
  3610.   each_full_file (copy_text);
  3611.   file_close ();
  3612.  
  3613.   if (trace_files)
  3614.     fprintf (stderr, "\n");
  3615.  
  3616.   padfile (text_pad, outdesc);
  3617. }
  3618.  
  3619. int
  3620. text_offset (entry)
  3621.      struct file_entry *entry;
  3622. {
  3623.   return entry->starting_offset + N_TXTOFF (entry->header);
  3624. }
  3625.  
  3626. /* Read in all of the relocation information */
  3627.  
  3628. void
  3629. read_relocation ()
  3630. {
  3631.   each_full_file (read_file_relocation);
  3632. }
  3633.  
  3634. /* Read in the relocation sections of ENTRY if necessary */
  3635.  
  3636. void
  3637. read_file_relocation (entry)
  3638.      struct file_entry *entry;
  3639. {
  3640.   register struct relocation_info *reloc;
  3641.   int desc;
  3642.   int read_return;
  3643.  
  3644.   desc = -1;
  3645.   if (!entry->textrel)
  3646.     {
  3647.       reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize);
  3648.       desc = file_open (entry);
  3649.       lseek (desc,
  3650.          text_offset (entry) + entry->header.a_text + entry->header.a_data,
  3651.          L_SET);
  3652.       if (entry->header.a_trsize != (read_return = read (desc, reloc, entry->header.a_trsize)))
  3653.     {
  3654.       fprintf (stderr, "Return from read: %d\n", read_return);
  3655.       fatal_with_file ("premature eof in text relocation of ", entry);
  3656.     }
  3657. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3658.       target_to_host_reloc_byte_order(reloc,
  3659.     entry->header.a_trsize/sizeof(*reloc));
  3660. #endif
  3661.       entry->textrel = reloc;
  3662.     }
  3663.  
  3664.   if (!entry->datarel)
  3665.     {
  3666.       reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize);
  3667.       if (desc == -1) desc = file_open (entry);
  3668.       lseek (desc,
  3669.          text_offset (entry) + entry->header.a_text
  3670.          + entry->header.a_data + entry->header.a_trsize,
  3671.          L_SET);
  3672.       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  3673.     fatal_with_file ("premature eof in data relocation of ", entry);
  3674. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3675.       target_to_host_reloc_byte_order(reloc,
  3676.     entry->header.a_drsize/sizeof(*reloc));
  3677. #endif
  3678.       entry->datarel = reloc;
  3679.     }
  3680. }
  3681.  
  3682. /* Read the text segment contents of ENTRY, relocate them,
  3683.    and write the result to the output file.
  3684.    If `-r', save the text relocation for later reuse.  */
  3685.  
  3686. void
  3687. copy_text (entry)
  3688.      struct file_entry *entry;
  3689. {
  3690.   register char *bytes;
  3691.   register int desc;
  3692.   register struct relocation_info *reloc;
  3693.  
  3694.   if (trace_files)
  3695.     prline_file_name (entry, stderr);
  3696.  
  3697.   desc = file_open (entry);
  3698.  
  3699.   /* Allocate space for the file's text section */
  3700.  
  3701.   bytes = (char *) alloca (entry->header.a_text);
  3702.  
  3703.   /* Deal with relocation information however is appropriate */
  3704.  
  3705.   if (entry->textrel)  reloc = entry->textrel;
  3706.   else if (relocatable_output)
  3707.     {
  3708.       read_file_relocation (entry);
  3709.       reloc = entry->textrel;
  3710.     }
  3711.   else
  3712.     {
  3713.       reloc = (struct relocation_info *) alloca (entry->header.a_trsize);
  3714.       lseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0);
  3715.       if (entry->header.a_trsize != read (desc, reloc, entry->header.a_trsize))
  3716.     fatal_with_file ("premature eof in text relocation of ", entry);
  3717. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3718.       target_to_host_reloc_byte_order(reloc,
  3719.       entry->header.a_trsize/sizeof(*reloc));
  3720. #endif
  3721.     }
  3722.  
  3723.   /* Read the text section into core.  */
  3724.  
  3725.   lseek (desc, text_offset (entry), 0);
  3726.   if (entry->header.a_text != read (desc, bytes, entry->header.a_text))
  3727.     fatal_with_file ("premature eof in text section of ", entry);
  3728.  
  3729.  
  3730.   /* Relocate the text according to the text relocation.  */
  3731.  
  3732.   perform_relocation (bytes, entry->text_start_address, entry->header.a_text,
  3733.               reloc, entry->header.a_trsize, entry);
  3734.  
  3735.   /* Write the relocated text to the output file.  */
  3736.  
  3737.   mywrite (bytes, 1, entry->header.a_text, outdesc);
  3738. }
  3739.  
  3740. /* Relocate the data segment of each input file
  3741.    and write to the output file.  */
  3742.  
  3743. void
  3744. write_data ()
  3745. {
  3746.   if (trace_files)
  3747.     fprintf (stderr, "Copying and relocating data:\n\n");
  3748.  
  3749.   each_full_file (copy_data);
  3750.   file_close ();
  3751.  
  3752.   /* Write out the set element vectors.  See digest symbols for
  3753.      description of length of the set vector section.  */
  3754.  
  3755. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3756.   {
  3757.     int i;
  3758.     for (i = 0; i < 2 * set_symbol_count + set_vector_count; ++i) {
  3759.     fix_byte_order(&set_vectors[i], sizeof(*set_vectors));
  3760.     }
  3761.   }
  3762. #endif
  3763.  
  3764.   if (set_vector_count)
  3765.     mywrite (set_vectors, 2 * set_symbol_count + set_vector_count,
  3766.          sizeof (unsigned long), outdesc);
  3767.  
  3768.   if (trace_files)
  3769.     fprintf (stderr, "\n");
  3770.  
  3771.   padfile (data_pad, outdesc);
  3772. }
  3773.  
  3774. /* Read the data segment contents of ENTRY, relocate them,
  3775.    and write the result to the output file.
  3776.    If `-r', save the data relocation for later reuse.
  3777.    See comments in `copy_text'.  */
  3778.  
  3779. void
  3780. copy_data (entry)
  3781.      struct file_entry *entry;
  3782. {
  3783.   register struct relocation_info *reloc;
  3784.   register char *bytes;
  3785.   register int desc;
  3786.  
  3787.   if (trace_files)
  3788.     prline_file_name (entry, stderr);
  3789.  
  3790.   desc = file_open (entry);
  3791.  
  3792.   bytes = (char *) alloca (entry->header.a_data);
  3793.  
  3794.   if (entry->datarel) reloc = entry->datarel;
  3795.   else if (relocatable_output)    /* Will need this again */
  3796.     {
  3797.       read_file_relocation (entry);
  3798.       reloc = entry->datarel;
  3799.     }
  3800.   else
  3801.     {
  3802.       reloc = (struct relocation_info *) alloca (entry->header.a_drsize);
  3803.       lseek (desc, text_offset (entry) + entry->header.a_text
  3804.          + entry->header.a_data + entry->header.a_trsize,
  3805.          0);
  3806.       if (entry->header.a_drsize != read (desc, reloc, entry->header.a_drsize))
  3807.     fatal_with_file ("premature eof in data relocation of ", entry);
  3808. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  3809.       target_to_host_reloc_byte_order(reloc,
  3810.       entry->header.a_drsize/sizeof(*reloc));
  3811. #endif
  3812.     }
  3813.  
  3814.   lseek (desc, text_offset (entry) + entry->header.a_text, 0);
  3815.   if (entry->header.a_data != read (desc, bytes, entry->header.a_data))
  3816.     fatal_with_file ("premature eof in data section of ", entry);
  3817.  
  3818.   perform_relocation (bytes, entry->data_start_address - entry->header.a_text,
  3819.               entry->header.a_data, reloc, entry->header.a_drsize, entry);
  3820.  
  3821.   mywrite (bytes, 1, entry->header.a_data, outdesc);
  3822. }
  3823.  
  3824. /* Relocate ENTRY's text or data section contents.
  3825.    DATA is the address of the contents, in core.
  3826.    DATA_SIZE is the length of the contents.
  3827.    PC_RELOCATION is the difference between the address of the contents
  3828.      in the output file and its address in the input file.
  3829.    RELOC_INFO is the address of the relocation info, in core.
  3830.    RELOC_SIZE is its length in bytes.  */
  3831. /* This version is about to be severley hacked by Randy.  Hope it
  3832.    works afterwards. */
  3833. void
  3834. perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size, entry)
  3835.      char *data;
  3836.      struct relocation_info *reloc_info;
  3837.      struct file_entry *entry;
  3838.      int pc_relocation;
  3839.      int data_size;
  3840.      int reloc_size;
  3841. {
  3842.   register struct relocation_info *p = reloc_info;
  3843.   struct relocation_info *end
  3844.     = reloc_info + reloc_size / sizeof (struct relocation_info);
  3845.   int text_relocation = entry->text_start_address;
  3846.   int data_relocation = entry->data_start_address - entry->header.a_text;
  3847.   int bss_relocation
  3848.     = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
  3849.  
  3850.   for (; p < end; p++)
  3851.     {
  3852.       register int relocation = 0;
  3853.       register int addr = RELOC_ADDRESS(p);
  3854.       register unsigned int mask = 0;
  3855.       register unsigned int x;
  3856.  
  3857.       if (addr >= data_size)
  3858.     fatal_with_file ("relocation address out of range in ", entry);
  3859.  
  3860.       if (RELOC_EXTERN_P(p))
  3861.     {
  3862.       int symindex = RELOC_SYMBOL (p) * sizeof (struct nlist);
  3863.       symbol *sp = ((symbol *)
  3864.             (((struct nlist *)
  3865.               (((char *)entry->symbols) + symindex))
  3866.              ->n_un.n_name));
  3867.  
  3868. #ifdef N_INDR
  3869.       /* Resolve indirection */
  3870.       if ((sp->defined & ~N_EXT) == N_INDR)
  3871.         sp = (symbol *) sp->value;
  3872. #endif
  3873.  
  3874.       if (symindex >= entry->header.a_syms)
  3875.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  3876.  
  3877.       /* If the symbol is undefined, leave it at zero.  */
  3878.       if (! sp->defined)
  3879.         relocation = 0;
  3880.       else
  3881.         relocation = sp->value;
  3882.     }
  3883.       else switch (RELOC_TYPE(p))
  3884.     {
  3885.     case N_TEXT:
  3886.     case N_TEXT | N_EXT:
  3887.       relocation = text_relocation;
  3888.       break;
  3889.  
  3890.     case N_DATA:
  3891.     case N_DATA | N_EXT:
  3892.       /* A word that points to beginning of the the data section
  3893.          initially contains not 0 but rather the "address" of that section
  3894.          in the input file, which is the length of the file's text.  */
  3895.       relocation = data_relocation;
  3896.       break;
  3897.  
  3898.     case N_BSS:
  3899.     case N_BSS | N_EXT:
  3900.       /* Similarly, an input word pointing to the beginning of the bss
  3901.          initially contains the length of text plus data of the file.  */
  3902.       relocation = bss_relocation;
  3903.       break;
  3904.  
  3905.     case N_ABS:
  3906.     case N_ABS | N_EXT:
  3907.       /* Don't know why this code would occur, but apparently it does.  */
  3908.       break;
  3909.  
  3910.     default:
  3911.       fatal_with_file ("nonexternal relocation code invalid in ", entry);
  3912.     }
  3913.  
  3914.       if (RELOC_PCREL_P(p))
  3915.     relocation -= pc_relocation;
  3916.  
  3917. #ifdef RELOC_ADD_EXTRA
  3918.       relocation += RELOC_ADD_EXTRA(p);
  3919.       if (relocatable_output)
  3920.     {
  3921.       /* If this RELOC_ADD_EXTRA is 0, it means that the
  3922.          symbol was external and the relocation does not
  3923.          need a fixup here.  */
  3924.       if (RELOC_ADD_EXTRA (p))
  3925.         {
  3926.           if (! RELOC_PCREL_P (p))
  3927.         RELOC_ADD_EXTRA (p) = relocation;
  3928.           else
  3929.         RELOC_ADD_EXTRA (p) -= pc_relocation;
  3930.         }
  3931. #if 0
  3932.       if (! RELOC_PCREL_P (p))
  3933.         {
  3934.           if ((int)p->r_type <= RELOC_32
  3935.           || RELOC_EXTERN_P (p) == 0)
  3936.         RELOC_ADD_EXTRA (p) = relocation;
  3937.         }
  3938.       else if (RELOC_EXTERN_P (p))
  3939.         RELOC_ADD_EXTRA (p) -= pc_relocation;
  3940. #endif
  3941.       continue;
  3942.     }
  3943. #endif
  3944.  
  3945.       relocation >>= RELOC_VALUE_RIGHTSHIFT(p);
  3946.  
  3947.       /* Unshifted mask for relocation */
  3948.       mask = 1 << RELOC_TARGET_BITSIZE(p) - 1;
  3949.       mask |= mask - 1;
  3950.       relocation &= mask;
  3951.  
  3952.       /* Shift everything up to where it's going to be used */
  3953.       relocation <<= RELOC_TARGET_BITPOS(p);
  3954.       mask <<= RELOC_TARGET_BITPOS(p);
  3955.  
  3956.       switch (RELOC_TARGET_SIZE(p))
  3957.     {
  3958.     case 0:
  3959.       if (RELOC_MEMORY_SUB_P(p))
  3960.         relocation -= mask & *(char *) (data + addr);
  3961.       else if (RELOC_MEMORY_ADD_P(p))
  3962.         relocation += mask & *(char *) (data + addr);
  3963.       *(char *) (data + addr) &= ~mask;
  3964.       *(char *) (data + addr) |= relocation;
  3965.       break;
  3966.  
  3967.     case 1:
  3968. #if TARGET_BYTE_ORDER==BIG_ENDIAN
  3969.       x = ((unsigned char *) (data + addr))[1]
  3970.        | (((unsigned char *) (data + addr))[0] << 8);
  3971. #endif
  3972. #if TARGET_BYTE_ORDER==LITTLE_ENDIAN
  3973.       x = ((unsigned char *) (data + addr))[0]
  3974.        | (((unsigned char *) (data + addr))[1] << 8);
  3975. #endif
  3976.       if (RELOC_MEMORY_SUB_P(p))
  3977.         relocation -= mask & x;
  3978.       else if (RELOC_MEMORY_ADD_P(p))
  3979.         relocation += mask & x;
  3980.       x &= ~mask;
  3981.       x |= relocation;
  3982. #if TARGET_BYTE_ORDER==BIG_ENDIAN
  3983.       ((unsigned char *) (data + addr))[1] = x;
  3984.       ((unsigned char *) (data + addr))[0] = x >> 8;
  3985. #endif
  3986. #if TARGET_BYTE_ORDER==LITTLE_ENDIAN
  3987.       ((unsigned char *) (data + addr))[0] = x;
  3988.       ((unsigned char *) (data + addr))[1] = x >> 8;
  3989. #endif
  3990.       break;
  3991.  
  3992.     case 2:
  3993. #if TARGET_BYTE_ORDER==BIG_ENDIAN
  3994.       x = ((unsigned char *) (data + addr))[3]
  3995.        | (((unsigned char *) (data + addr))[2] << 8)
  3996.        | (((unsigned char *) (data + addr))[1] << 16)
  3997.        | (((unsigned char *) (data + addr))[0] << 24);
  3998. #endif
  3999. #if TARGET_BYTE_ORDER==LITTLE_ENDIAN
  4000.       x = ((unsigned char *) (data + addr))[0]
  4001.        | (((unsigned char *) (data + addr))[1] << 8)
  4002.        | (((unsigned char *) (data + addr))[2] << 16)
  4003.        | (((unsigned char *) (data + addr))[3] << 24);
  4004. #endif
  4005.           if (RELOC_MEMORY_SUB_P(p))
  4006.         relocation -= mask & x;
  4007.       else if (RELOC_MEMORY_ADD_P(p))
  4008.         relocation += mask & x;
  4009.       x &= ~mask;
  4010.       x |= relocation;
  4011. #if TARGET_BYTE_ORDER==BIG_ENDIAN
  4012.       ((unsigned char *) (data + addr))[3] = x;
  4013.       ((unsigned char *) (data + addr))[2] = x >> 8;
  4014.       ((unsigned char *) (data + addr))[1] = x >> 16;
  4015.       ((unsigned char *) (data + addr))[0] = x >> 24;
  4016. #endif
  4017. #if TARGET_BYTE_ORDER==LITTLE_ENDIAN
  4018.       ((unsigned char *) (data + addr))[0] = x;
  4019.       ((unsigned char *) (data + addr))[1] = x >> 8;
  4020.       ((unsigned char *) (data + addr))[2] = x >> 16;
  4021.       ((unsigned char *) (data + addr))[3] = x >> 24;
  4022. #endif
  4023.       break;
  4024.  
  4025.     default:
  4026.       fatal_with_file ("Unimplemented relocation field length in ", entry);
  4027.     }
  4028.     }
  4029. }
  4030.  
  4031. /* For relocatable_output only: write out the relocation,
  4032.    relocating the addresses-to-be-relocated.  */
  4033.  
  4034. void coptxtrel (), copdatrel ();
  4035.  
  4036. void
  4037. write_rel ()
  4038. {
  4039.   register int i;
  4040.   register int count = 0;
  4041.  
  4042.   if (trace_files)
  4043.     fprintf (stderr, "Writing text relocation:\n\n");
  4044.  
  4045.   /* Assign each global symbol a sequence number, giving the order
  4046.      in which `write_syms' will write it.
  4047.      This is so we can store the proper symbolnum fields
  4048.      in relocation entries we write.  */
  4049.  
  4050.   for (i = 0; i < TABSIZE; i++)
  4051.     {
  4052.       symbol *sp;
  4053.       for (sp = symtab[i]; sp; sp = sp->link)
  4054.     if (sp->referenced || sp->defined)
  4055.       {
  4056.         sp->def_count = count++;
  4057.         /* Leave room for the reference required by N_INDR, if
  4058.            necessary.  */
  4059.         if ((sp->defined & ~N_EXT) == N_INDR)
  4060.           count++;
  4061.       }
  4062.     }
  4063.   /* Correct, because if (reloatable_output), we will also be writing
  4064.      whatever indirect blocks we have.  */
  4065.   if (count != defined_global_sym_count
  4066.       + undefined_global_sym_count + global_indirect_count)
  4067.     fatal ("internal error");
  4068.  
  4069.   /* Write out the relocations of all files, remembered from copy_text.  */
  4070.  
  4071.   each_full_file (coptxtrel);
  4072.  
  4073.   if (trace_files)
  4074.     fprintf (stderr, "\nWriting data relocation:\n\n");
  4075.  
  4076.   each_full_file (copdatrel);
  4077.  
  4078.   if (trace_files)
  4079.     fprintf (stderr, "\n");
  4080. }
  4081.  
  4082. void
  4083. coptxtrel (entry)
  4084.      struct file_entry *entry;
  4085. {
  4086.   register struct relocation_info *p, *end;
  4087.   register int reloc = entry->text_start_address;
  4088.  
  4089.   p = entry->textrel;
  4090.   end = (struct relocation_info *) (entry->header.a_trsize + (char *) p);
  4091.   while (p < end)
  4092.     {
  4093.       RELOC_ADDRESS(p) += reloc;
  4094.       if (RELOC_EXTERN_P(p))
  4095.     {
  4096.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  4097.       symbol *symptr = ((symbol *)
  4098.                 (((struct nlist *)
  4099.                   (((char *)entry->symbols) + symindex))
  4100.                  ->n_un.n_name));
  4101.  
  4102.       if (symindex >= entry->header.a_syms)
  4103.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  4104.  
  4105. #ifdef N_INDR
  4106.       /* Resolve indirection.  */
  4107.       if ((symptr->defined & ~N_EXT) == N_INDR)
  4108.         symptr = (symbol *) symptr->value;
  4109. #endif
  4110.  
  4111.       /* If the symbol is now defined, change the external relocation
  4112.          to an internal one.  */
  4113.  
  4114.       if (symptr->defined)
  4115.         {
  4116.           RELOC_EXTERN_P(p) = 0;
  4117.           RELOC_SYMBOL(p) = (symptr->defined & N_TYPE);
  4118. #ifdef RELOC_ADD_EXTRA
  4119.           /* If we aren't going to be adding in the value in
  4120.              memory on the next pass of the loader, then we need
  4121.          to add it in from the relocation entry.  Otherwise
  4122.              the work we did in this pass is lost.  */
  4123.           if (!RELOC_MEMORY_ADD_P(p))
  4124.         RELOC_ADD_EXTRA (p) += symptr->value;
  4125. #endif
  4126.         }
  4127.       else
  4128.         /* Debugger symbols come first, so have to start this
  4129.            after them.  */
  4130.           RELOC_SYMBOL(p) = (symptr->def_count + nsyms
  4131.                  - defined_global_sym_count
  4132.                  - undefined_global_sym_count
  4133.                  - global_indirect_count);
  4134.     }
  4135.       p++;
  4136.     }
  4137. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4138.     host_to_target_reloc_byte_order((struct relocation_info *) entry->textrel,
  4139.     entry->header.a_trsize/sizeof(struct relocation_info));
  4140. #endif
  4141.   mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc);
  4142. }
  4143.  
  4144. void
  4145. copdatrel (entry)
  4146.      struct file_entry *entry;
  4147. {
  4148.   register struct relocation_info *p, *end;
  4149.   /* Relocate the address of the relocation.
  4150.      Old address is relative to start of the input file's data section.
  4151.      New address is relative to start of the output file's data section.  */
  4152.   register int reloc = entry->data_start_address - text_size;
  4153.  
  4154.   p = entry->datarel;
  4155.   end = (struct relocation_info *) (entry->header.a_drsize + (char *) p);
  4156.   while (p < end)
  4157.     {
  4158.       RELOC_ADDRESS(p) += reloc;
  4159.       if (RELOC_EXTERN_P(p))
  4160.     {
  4161.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  4162.       symbol *symptr = ((symbol *)
  4163.                 (((struct nlist *)
  4164.                   (((char *)entry->symbols) + symindex))
  4165.                  ->n_un.n_name));
  4166.       int symtype;
  4167.  
  4168.       if (symindex >= entry->header.a_syms)
  4169.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  4170.  
  4171. #ifdef N_INDR
  4172.       /* Resolve indirection.  */
  4173.       if ((symptr->defined & ~N_EXT) == N_INDR)
  4174.         symptr = (symbol *) symptr->value;
  4175. #endif
  4176.  
  4177.        symtype = symptr->defined & N_TYPE;
  4178.  
  4179.       if (force_common_definition
  4180.           || symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS)
  4181.         {
  4182.           RELOC_EXTERN_P(p) = 0;
  4183.           RELOC_SYMBOL(p) = symtype;
  4184.         }
  4185.       else
  4186.         /* Debugger symbols come first, so have to start this
  4187.            after them.  */
  4188.         RELOC_SYMBOL(p)
  4189.           = (((symbol *)
  4190.           (((struct nlist *)
  4191.             (((char *)entry->symbols) + symindex))
  4192.            ->n_un.n_name))
  4193.          ->def_count
  4194.          + nsyms - defined_global_sym_count
  4195.          - undefined_global_sym_count
  4196.          - global_indirect_count);
  4197.     }
  4198.       p++;
  4199.     }
  4200. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4201.     host_to_target_reloc_byte_order((struct relocation_info *) entry->datarel,
  4202.     entry->header.a_drsize/sizeof(struct relocation_info));
  4203. #endif
  4204.   mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc);
  4205. }
  4206.  
  4207. void write_file_syms ();
  4208. void write_string_table ();
  4209.  
  4210. /* Offsets and current lengths of symbol and string tables in output file. */
  4211.  
  4212. int symbol_table_offset;
  4213. int symbol_table_len;
  4214.  
  4215. /* Address in output file where string table starts.  */
  4216. int string_table_offset;
  4217.  
  4218. /* Offset within string table
  4219.    where the strings in `strtab_vector' should be written.  */
  4220. int string_table_len;
  4221.  
  4222. /* Total size of string table strings allocated so far,
  4223.    including strings in `strtab_vector'.  */
  4224. int strtab_size;
  4225.  
  4226. /* Vector whose elements are strings to be added to the string table.  */
  4227. char **strtab_vector;
  4228.  
  4229. /* Vector whose elements are the lengths of those strings.  */
  4230. int *strtab_lens;
  4231.  
  4232. /* Index in `strtab_vector' at which the next string will be stored.  */
  4233. int strtab_index;
  4234.  
  4235. /* Add the string NAME to the output file string table.
  4236.    Record it in `strtab_vector' to be output later.
  4237.    Return the index within the string table that this string will have.  */
  4238.  
  4239. int
  4240. assign_string_table_index (name)
  4241.      char *name;
  4242. {
  4243.   register int index = strtab_size;
  4244.   register int len = strlen (name) + 1;
  4245.  
  4246.   strtab_size += len;
  4247.   strtab_vector[strtab_index] = name;
  4248.   strtab_lens[strtab_index++] = len;
  4249.  
  4250.   return index;
  4251. }
  4252.  
  4253. FILE *outstream = (FILE *) 0;
  4254.  
  4255. /* Write the contents of `strtab_vector' into the string table.
  4256.    This is done once for each file's local&debugger symbols
  4257.    and once for the global symbols.  */
  4258.  
  4259. void
  4260. write_string_table ()
  4261. {
  4262.   register int i;
  4263.  
  4264.   lseek (outdesc, string_table_offset + string_table_len, 0);
  4265.  
  4266.   if (!outstream)
  4267.     outstream = fdopen (outdesc, "w");
  4268.  
  4269.   for (i = 0; i < strtab_index; i++)
  4270.     {
  4271.       fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
  4272.       string_table_len += strtab_lens[i];
  4273.     }
  4274.  
  4275.   fflush (outstream);
  4276.  
  4277.   /* Report I/O error such as disk full.  */
  4278.   if (ferror (outstream))
  4279.     perror_name (output_filename);
  4280. }
  4281.  
  4282. /* Write the symbol table and string table of the output file.  */
  4283.  
  4284. void
  4285. write_syms ()
  4286. {
  4287.   /* Number of symbols written so far.  */
  4288.   int syms_written = 0;
  4289.   register int i;
  4290.   register symbol *sp;
  4291.  
  4292.   /* Buffer big enough for all the global symbols.  One
  4293.      extra struct for each indirect symbol to hold the extra reference
  4294.      following. */
  4295.   struct nlist *buf
  4296.     = (struct nlist *) alloca ((defined_global_sym_count
  4297.                 + undefined_global_sym_count
  4298.                 + global_indirect_count)
  4299.                    * sizeof (struct nlist));
  4300.   /* Pointer for storing into BUF.  */
  4301.   register struct nlist *bufp = buf;
  4302.  
  4303.   /* Size of string table includes the bytes that store the size.  */
  4304.   strtab_size = sizeof strtab_size;
  4305.  
  4306.   symbol_table_offset = N_SYMOFF (outheader);
  4307.   symbol_table_len = 0;
  4308.   string_table_offset = N_STROFF (outheader);
  4309.   string_table_len = strtab_size;
  4310.  
  4311.   if (strip_symbols == STRIP_ALL)
  4312.     return;
  4313.  
  4314.   /* Write the local symbols defined by the various files.  */
  4315.  
  4316.   each_file (write_file_syms, &syms_written);
  4317.   file_close ();
  4318.  
  4319.   /* Now write out the global symbols.  */
  4320.  
  4321.   /* Allocate two vectors that record the data to generate the string
  4322.      table from the global symbols written so far.  This must include
  4323.      extra space for the references following indirect outputs. */
  4324.  
  4325.   strtab_vector = (char **) alloca ((num_hash_tab_syms
  4326.                      + global_indirect_count) * sizeof (char *));
  4327.   strtab_lens = (int *) alloca ((num_hash_tab_syms
  4328.                  + global_indirect_count) * sizeof (int));
  4329.   strtab_index = 0;
  4330.  
  4331.   /* Scan the symbol hash table, bucket by bucket.  */
  4332.  
  4333.   for (i = 0; i < TABSIZE; i++)
  4334.     for (sp = symtab[i]; sp; sp = sp->link)
  4335.       {
  4336.     struct nlist nl;
  4337.  
  4338.     nl.n_other = 0;
  4339.     nl.n_desc = 0;
  4340.  
  4341.     /* Compute a `struct nlist' for the symbol.  */
  4342.  
  4343.     if (sp->defined || sp->referenced)
  4344.       {
  4345.         /* common condition needs to be before undefined condition */
  4346.         /* because unallocated commons are set undefined in */
  4347.         /* digest_symbols */
  4348.         if (sp->defined > 1) /* defined with known type */
  4349.           {
  4350.         /* If the target of an indirect symbol has been
  4351.            defined and we are outputting an executable,
  4352.            resolve the indirection; it's no longer needed */
  4353.         if (!relocatable_output
  4354.             && ((sp->defined & N_TYPE) == N_INDR)
  4355.             && (((symbol *) sp->value)->defined > 1))
  4356.           {
  4357.             symbol *newsp = (symbol *) sp->value;
  4358.             nl.n_type = newsp->defined;
  4359.             nl.n_value = newsp->value;
  4360.           }
  4361.         else
  4362.           {
  4363.             nl.n_type = sp->defined;
  4364.             if (sp->defined != (N_INDR | N_EXT))
  4365.               nl.n_value = sp->value;
  4366.             else
  4367.               nl.n_value = 0;
  4368.           }
  4369.           }
  4370.         else if (sp->max_common_size) /* defined as common but not allocated. */
  4371.           {
  4372.         /* happens only with -r and not -d */
  4373.         /* write out a common definition */
  4374.         nl.n_type = N_UNDF | N_EXT;
  4375.         nl.n_value = sp->max_common_size;
  4376.           }
  4377.         else if (!sp->defined)          /* undefined -- legit only if -r */
  4378.           {
  4379.         nl.n_type = N_UNDF | N_EXT;
  4380.         nl.n_value = 0;
  4381.           }
  4382.         else
  4383.           fatal ("internal error: %s defined in mysterious way", sp->name);
  4384.  
  4385.         /* Allocate string table space for the symbol name.  */
  4386.  
  4387.         nl.n_un.n_strx = assign_string_table_index (sp->name);
  4388.  
  4389.         /* Output to the buffer and count it.  */
  4390.  
  4391.         *bufp++ = nl;
  4392.         syms_written++;
  4393.         if (nl.n_type == (N_INDR | N_EXT))
  4394.           {
  4395.         struct nlist xtra_ref;
  4396.         xtra_ref.n_type == N_EXT | N_UNDF;
  4397.         xtra_ref.n_un.n_strx
  4398.           = assign_string_table_index (((symbol *) sp->value)->name);
  4399.         xtra_ref.n_other = 0;
  4400.         xtra_ref.n_desc = 0;
  4401.         xtra_ref.n_value = 0;
  4402.         *bufp++ = xtra_ref;
  4403.         syms_written++;
  4404.           }
  4405.       }
  4406.       }
  4407.  
  4408.   /* Output the buffer full of `struct nlist's.  */
  4409.  
  4410.   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  4411. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4412.   {
  4413.     int i;
  4414.  
  4415.     for (i = 0; i < bufp - buf; ++i)
  4416.       {
  4417.         fix_byte_order(&buf[i].n_un.n_name, sizeof(buf->n_un.n_name));
  4418.     fix_byte_order(&buf[i].n_desc, sizeof(buf->n_desc));
  4419.     fix_byte_order(&buf[i].n_value, sizeof(buf->n_value));
  4420.       }
  4421.   }
  4422. #endif
  4423.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4424.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  4425.  
  4426.   if (syms_written != nsyms)
  4427.     fatal ("internal error: wrong number of symbols written into output file", 0);
  4428.  
  4429.   if (symbol_table_offset + symbol_table_len != string_table_offset)
  4430.     fatal ("internal error: inconsistent symbol table length", 0);
  4431.  
  4432.   /* Now the total string table size is known, so write it.
  4433.      We are already positioned at the right place in the file.  */
  4434.  
  4435. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4436.   fix_byte_order(&strtab_size, sizeof(strtab_size));
  4437. #endif
  4438.  
  4439.   mywrite (&strtab_size, sizeof (int), 1, outdesc);  /* we're at right place */
  4440.  
  4441.   /* Write the strings for the global symbols.  */
  4442.  
  4443.   write_string_table ();
  4444. }
  4445.  
  4446. /* Write the local and debugger symbols of file ENTRY.
  4447.    Increment *SYMS_WRITTEN_ADDR for each symbol that is written.  */
  4448.  
  4449. /* Note that we do not combine identical names of local symbols.
  4450.    dbx or gdb would be confused if we did that.  */
  4451.  
  4452. void
  4453. write_file_syms (entry, syms_written_addr)
  4454.      struct file_entry *entry;
  4455.      int *syms_written_addr;
  4456. {
  4457.   register struct nlist *p = entry->symbols;
  4458.   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  4459.  
  4460.   /* Buffer to accumulate all the syms before writing them.
  4461.      It has one extra slot for the local symbol we generate here.  */
  4462.   struct nlist *buf
  4463.     = (struct nlist *) alloca (entry->header.a_syms + sizeof (struct nlist));
  4464.   register struct nlist *bufp = buf;
  4465.  
  4466.   /* Upper bound on number of syms to be written here.  */
  4467.   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
  4468.  
  4469.   /* Make tables that record, for each symbol, its name and its name's length.
  4470.      The elements are filled in by `assign_string_table_index'.  */
  4471.  
  4472.   strtab_vector = (char **) alloca (max_syms * sizeof (char *));
  4473.   strtab_lens = (int *) alloca (max_syms * sizeof (int));
  4474.   strtab_index = 0;
  4475.  
  4476.   /* Generate a local symbol for the start of this file's text.  */
  4477.  
  4478.   if (discard_locals != DISCARD_ALL)
  4479.     {
  4480.       struct nlist nl;
  4481.  
  4482.       nl.n_type = N_TEXT;
  4483.       nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name);
  4484.       nl.n_value = entry->text_start_address;
  4485.       nl.n_desc = 0;
  4486.       nl.n_other = 0;
  4487.       *bufp++ = nl;
  4488.       (*syms_written_addr)++;
  4489.       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  4490.     }
  4491.  
  4492.   /* Read the file's string table.  */
  4493.  
  4494.   entry->strings = (char *) alloca (entry->string_size);
  4495.   read_entry_strings (file_open (entry), entry);
  4496.  
  4497.   for (; p < end; p++)
  4498.     {
  4499.       register int type = p->n_type;
  4500.       register int write = 0;
  4501.  
  4502.       /* WRITE gets 1 for a non-global symbol that should be written.  */
  4503.  
  4504.  
  4505.       if (SET_ELEMENT_P (type))    /* This occurs even if global.  These */
  4506.                 /* types of symbols are never written */
  4507.                 /* globally, though they are stored */
  4508.                 /* globally.  */
  4509.         write = relocatable_output;
  4510.       else if (!(type & (N_STAB | N_EXT)))
  4511.         /* ordinary local symbol */
  4512.     write = ((discard_locals != DISCARD_ALL)
  4513.          && !(discard_locals == DISCARD_L &&
  4514.               (p->n_un.n_strx + entry->strings)[0] == LPREFIX)
  4515.          && type != N_WARNING);
  4516.       else if (!(type & N_EXT))
  4517.     /* debugger symbol */
  4518.         write = (strip_symbols == STRIP_NONE);
  4519.  
  4520.       if (write)
  4521.     {
  4522.       /* If this symbol has a name,
  4523.          allocate space for it in the output string table.  */
  4524.  
  4525.       if (p->n_un.n_strx)
  4526.         p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx
  4527.                             + entry->strings);
  4528.  
  4529.       /* Output this symbol to the buffer and count it.  */
  4530.  
  4531.       *bufp++ = *p;
  4532.       (*syms_written_addr)++;
  4533.     }
  4534.     }
  4535.  
  4536.   /* All the symbols are now in BUF; write them.  */
  4537.  
  4538.   lseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  4539. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4540.   {
  4541.     int i;
  4542.  
  4543.     for (i = 0; i < bufp - buf; ++i)
  4544.       {
  4545.     fix_byte_order(&buf[i].n_un.n_name, sizeof(buf->n_un.n_name));
  4546.     fix_byte_order(&buf[i].n_desc, sizeof(buf->n_desc));
  4547.     fix_byte_order(&buf[i].n_value, sizeof(buf->n_value));
  4548.       }
  4549.   }
  4550. #endif
  4551.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  4552.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  4553.  
  4554.   /* Write the string-table data for the symbols just written,
  4555.      using the data in vectors `strtab_vector' and `strtab_lens'.  */
  4556.  
  4557.   write_string_table ();
  4558.   entry->strings = 0;        /* Since it will dissapear anyway.  */
  4559. }
  4560.  
  4561. /* Copy any GDB symbol segments from the input files to the output file.
  4562.    The contents of the symbol segment is copied without change
  4563.    except that we store some information into the beginning of it.  */
  4564.  
  4565. void write_file_symseg ();
  4566.  
  4567. void
  4568. write_symsegs ()
  4569. {
  4570.   each_file (write_file_symseg, 0);
  4571. }
  4572.  
  4573. void
  4574. write_file_symseg (entry)
  4575.      struct file_entry *entry;
  4576. {
  4577.   char buffer[4096];
  4578.   struct symbol_root root;
  4579.   int indesc;
  4580.   int len;
  4581.  
  4582.   if (entry->symseg_offset == 0)
  4583.     return;
  4584.  
  4585.   /* This entry has a symbol segment.  Read the root of the segment.  */
  4586.  
  4587.   indesc = file_open (entry);
  4588.   lseek (indesc, entry->symseg_offset + entry->starting_offset, 0);
  4589.   if (sizeof root != read (indesc, &root, sizeof root))
  4590.     fatal_with_file ("premature end of file in symbol segment of ", entry);
  4591.  
  4592. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4593.   fix_symbol_root_byte_order(&root);
  4594. #endif
  4595.  
  4596.   /* Store some relocation info into the root.  */
  4597.  
  4598.   root.ldsymoff = entry->local_syms_offset;
  4599.   root.textrel = entry->text_start_address;
  4600.   root.datarel = entry->data_start_address - entry->header.a_text;
  4601.   root.bssrel = entry->bss_start_address
  4602.     - entry->header.a_text - entry->header.a_data;
  4603.   root.databeg = entry->data_start_address - root.datarel;
  4604.   root.bssbeg = entry->bss_start_address - root.bssrel;
  4605.  
  4606.   /* Write the modified root into the output file.  */
  4607.  
  4608. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4609.   fix_symbol_root_byte_order(&root);
  4610. #endif
  4611.   mywrite (&root, sizeof root, 1, outdesc);
  4612.  
  4613.   /* Copy the rest of the symbol segment unchanged.  */
  4614.  
  4615.   if (entry->superfile)
  4616.     {
  4617.       /* Library member: number of bytes to copy is determined
  4618.      from the member's total size.  */
  4619.  
  4620.       int total = entry->total_size - entry->symseg_offset - sizeof root;
  4621.  
  4622.       while (total > 0)
  4623.     {
  4624.       len = read (indesc, buffer, min (sizeof buffer, total));
  4625.  
  4626.       if (len != min (sizeof buffer, total))
  4627.         fatal_with_file ("premature end of file in symbol segment of ", entry);
  4628.       total -= len;
  4629.       mywrite (buffer, len, 1, outdesc);
  4630.     }
  4631.     }
  4632.   else
  4633.     {
  4634.       /* A separate file: copy until end of file.  */
  4635.  
  4636.       while (len = read (indesc, buffer, sizeof buffer))
  4637.     {
  4638.       mywrite (buffer, len, 1, outdesc);
  4639.       if (len < sizeof buffer)
  4640.         break;
  4641.     }
  4642.     }
  4643.  
  4644.   file_close ();
  4645. }
  4646.  
  4647. /* Create the symbol table entries for `etext', `edata' and `end'.  */
  4648.  
  4649. void
  4650. symtab_init ()
  4651. {
  4652. #ifndef nounderscore
  4653.   edata_symbol = getsym ("_edata");
  4654.   etext_symbol = getsym ("_etext");
  4655.   end_symbol = getsym ("_end");
  4656. #else
  4657.   edata_symbol = getsym ("edata");
  4658.   etext_symbol = getsym ("etext");
  4659.   end_symbol = getsym ("end");
  4660. #endif
  4661.  
  4662. #if TARGET_MACHINE==TARGET_SUN4 || TARGET_MACHINE==TARGET_SUN3 || TARGET_MACHINE==TARGET_SUN2
  4663.   {
  4664.     symbol *dynamic_symbol = getsym ("__DYNAMIC");
  4665.     dynamic_symbol->defined = N_ABS | N_EXT;
  4666.     dynamic_symbol->referenced = 1;
  4667.     dynamic_symbol->value = 0;
  4668.   }
  4669. #endif
  4670. #if TARGET_MACHINE==TARGET_SEQUENT
  4671.   {
  4672.     symbol *_387_flt_symbol = getsym ("_387_flt");
  4673.     _387_flt_symbol->defined = N_ABS | N_EXT;
  4674.     _387_flt_symbol->referenced = 1;
  4675.     _387_flt_symbol->value = 0;
  4676.   }
  4677. #endif
  4678.  
  4679.   edata_symbol->defined = N_DATA | N_EXT;
  4680.   etext_symbol->defined = N_TEXT | N_EXT;
  4681.   end_symbol->defined = N_BSS | N_EXT;
  4682.  
  4683.   edata_symbol->referenced = 1;
  4684.   etext_symbol->referenced = 1;
  4685.   end_symbol->referenced = 1;
  4686. }
  4687.  
  4688. /* Compute the hash code for symbol name KEY.  */
  4689.  
  4690. int
  4691. hash_string (key)
  4692.      char *key;
  4693. {
  4694.   register char *cp;
  4695.   register int k;
  4696.  
  4697.   cp = key;
  4698.   k = 0;
  4699.   while (*cp)
  4700.     k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
  4701.  
  4702.   return k;
  4703. }
  4704.  
  4705. /* Get the symbol table entry for the global symbol named KEY.
  4706.    Create one if there is none.  */
  4707.  
  4708. symbol *
  4709. getsym (key)
  4710.      char *key;
  4711. {
  4712.   register int hashval;
  4713.   register symbol *bp;
  4714.  
  4715.   /* Determine the proper bucket.  */
  4716.  
  4717.   hashval = hash_string (key) % TABSIZE;
  4718.  
  4719.   /* Search the bucket.  */
  4720.  
  4721.   for (bp = symtab[hashval]; bp; bp = bp->link)
  4722.     if (! strcmp (key, bp->name))
  4723.       return bp;
  4724.  
  4725.   /* Nothing was found; create a new symbol table entry.  */
  4726.  
  4727.   bp = (symbol *) xmalloc (sizeof (symbol));
  4728.   bp->refs = 0;
  4729.   bp->name = (char *) xmalloc (strlen (key) + 1);
  4730.   strcpy (bp->name, key);
  4731.   bp->defined = 0;
  4732.   bp->referenced = 0;
  4733.   bp->trace = 0;
  4734.   bp->value = 0;
  4735.   bp->max_common_size = 0;
  4736.   bp->warning = 0;
  4737.   bp->undef_refs = 0;
  4738.   bp->multiply_defined = 0;
  4739.  
  4740.   /* Add the entry to the bucket.  */
  4741.  
  4742.   bp->link = symtab[hashval];
  4743.   symtab[hashval] = bp;
  4744.  
  4745.   ++num_hash_tab_syms;
  4746.  
  4747.   return bp;
  4748. }
  4749.  
  4750. /* Like `getsym' but return 0 if the symbol is not already known.  */
  4751.  
  4752. symbol *
  4753. getsym_soft (key)
  4754.      char *key;
  4755. {
  4756.   register int hashval;
  4757.   register symbol *bp;
  4758.  
  4759.   /* Determine which bucket.  */
  4760.  
  4761.   hashval = hash_string (key) % TABSIZE;
  4762.  
  4763.   /* Search the bucket.  */
  4764.  
  4765.   for (bp = symtab[hashval]; bp; bp = bp->link)
  4766.     if (! strcmp (key, bp->name))
  4767.       return bp;
  4768.  
  4769.   return 0;
  4770. }
  4771.  
  4772. /* Report a fatal error.
  4773.    STRING is a printf format string and ARG is one arg for it.  */
  4774.  
  4775. void
  4776. fatal (string, arg)
  4777.      char *string, *arg;
  4778. {
  4779.   fprintf (stderr, "ld: ");
  4780.   fprintf (stderr, string, arg);
  4781.   fprintf (stderr, "\n");
  4782.   exit (1);
  4783. }
  4784.  
  4785. /* Report a fatal error.  The error message is STRING
  4786.    followed by the filename of ENTRY.  */
  4787.  
  4788. void
  4789. fatal_with_file (string, entry)
  4790.      char *string;
  4791.      struct file_entry *entry;
  4792. {
  4793.   fprintf (stderr, "ld: ");
  4794.   fprintf (stderr, string);
  4795.   print_file_name (entry, stderr);
  4796.   fprintf (stderr, "\n");
  4797.   exit (1);
  4798. }
  4799.  
  4800. /* Report a fatal error using the message for the last failed system call,
  4801.    followed by the string NAME.  */
  4802.  
  4803. void
  4804. perror_name (name)
  4805.      char *name;
  4806. {
  4807.   extern int errno, sys_nerr;
  4808.   extern char *sys_errlist[];
  4809.   char *s;
  4810.  
  4811.   if (errno < sys_nerr)
  4812.     s = concat ("", sys_errlist[errno], " for %s");
  4813.   else
  4814.     s = "cannot open %s";
  4815.   fatal (s, name);
  4816. }
  4817.  
  4818. /* Report a fatal error using the message for the last failed system call,
  4819.    followed by the name of file ENTRY.  */
  4820.  
  4821. void
  4822. perror_file (entry)
  4823.      struct file_entry *entry;
  4824. {
  4825.   extern int errno, sys_nerr;
  4826.   extern char *sys_errlist[];
  4827.   char *s;
  4828.  
  4829.   if (errno < sys_nerr)
  4830.     s = concat ("", sys_errlist[errno], " for ");
  4831.   else
  4832.     s = "cannot open ";
  4833.   fatal_with_file (s, entry);
  4834. }
  4835.  
  4836. /* Report a nonfatal error.
  4837.    STRING is a format for printf, and ARG1 ... ARG3 are args for it.  */
  4838.  
  4839. void
  4840. error (string, arg1, arg2, arg3)
  4841.      char *string, *arg1, *arg2, *arg3;
  4842. {
  4843.   fprintf (stderr, "%s: ", progname);
  4844.   fprintf (stderr, string, arg1, arg2, arg3);
  4845.   fprintf (stderr, "\n");
  4846. }
  4847.  
  4848.  
  4849. /* Output COUNT*ELTSIZE bytes of data at BUF
  4850.    to the descriptor DESC.  */
  4851.  
  4852. void
  4853. mywrite (buf, count, eltsize, desc)
  4854.      char *buf;
  4855.      int count;
  4856.      int eltsize;
  4857.      int desc;
  4858. {
  4859.   register int val;
  4860.   register int bytes = count * eltsize;
  4861.  
  4862.   while (bytes > 0)
  4863.     {
  4864.       val = write (desc, buf, bytes);
  4865.       if (val <= 0)
  4866.     perror_name (output_filename);
  4867.       buf += val;
  4868.       bytes -= val;
  4869.     }
  4870. }
  4871.  
  4872. /* Output PADDING zero-bytes to descriptor OUTDESC.
  4873.    PADDING may be negative; in that case, do nothing.  */
  4874.  
  4875. void
  4876. padfile (padding, outdesc)
  4877.      int padding;
  4878.      int outdesc;
  4879. {
  4880.   register char *buf;
  4881.   if (padding <= 0)
  4882.     return;
  4883.  
  4884.   buf = (char *) alloca (padding);
  4885.   bzero (buf, padding);
  4886.   mywrite (buf, padding, 1, outdesc);
  4887. }
  4888.  
  4889. /* Return a newly-allocated string
  4890.    whose contents concatenate the strings S1, S2, S3.  */
  4891.  
  4892. char *
  4893. concat (s1, s2, s3)
  4894.      char *s1, *s2, *s3;
  4895. {
  4896.   register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  4897.   register char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  4898.  
  4899.   strcpy (result, s1);
  4900.   strcpy (result + len1, s2);
  4901.   strcpy (result + len1 + len2, s3);
  4902.   result[len1 + len2 + len3] = 0;
  4903.  
  4904.   return result;
  4905. }
  4906.  
  4907. /* Parse the string ARG using scanf format FORMAT, and return the result.
  4908.    If it does not parse, report fatal error
  4909.    generating the error message using format string ERROR and ARG as arg.  */
  4910.  
  4911. int
  4912. parse (arg, format, error)
  4913.      char *arg, *format;
  4914. {
  4915.   int x;
  4916.   if (1 != sscanf (arg, format, &x))
  4917.     fatal (error, arg);
  4918.   return x;
  4919. }
  4920.  
  4921. /* Like malloc but get fatal error if memory is exhausted.  */
  4922.  
  4923. int
  4924. xmalloc (size)
  4925.      int size;
  4926. {
  4927.   register int result = malloc (size);
  4928.   if (!result)
  4929.     fatal ("virtual memory exhausted", 0);
  4930.   return result;
  4931. }
  4932.  
  4933. /* Like realloc but get fatal error if memory is exhausted.  */
  4934.  
  4935. int
  4936. xrealloc (ptr, size)
  4937.      char *ptr;
  4938.      int size;
  4939. {
  4940.   register int result = realloc (ptr, size);
  4941.   if (!result)
  4942.     fatal ("virtual memory exhausted", 0);
  4943.   return result;
  4944. }
  4945.  
  4946. #ifdef USG
  4947.  
  4948. void
  4949. bzero (p, n)
  4950.      char *p;
  4951. {
  4952.   memset (p, 0, n);
  4953. }
  4954.  
  4955. void
  4956. bcopy (from, to, n)
  4957.      char *from, *to;
  4958. {
  4959.   memcpy (to, from, n);
  4960. }
  4961.  
  4962. getpagesize ()
  4963. {
  4964.   return (4096);
  4965. }
  4966.  
  4967. #endif
  4968.  
  4969.  
  4970. #if HOST_BYTE_ORDER != TARGET_BYTE_ORDER
  4971.  
  4972. static void
  4973. fix_byte_order(p, n)
  4974.     char *p;
  4975.     int n;
  4976. {
  4977.     char t;
  4978.  
  4979.     switch (n) {
  4980.  
  4981.     case 1:
  4982.     return;
  4983.  
  4984.     case 2:
  4985.     t = p[0];
  4986.     p[0] = p[1];
  4987.     p[1] = t;
  4988.     return;
  4989.  
  4990.     case 4:
  4991.     t = p[0];
  4992.     p[0] = p[3];
  4993.     p[3] = t;
  4994.     t = p[1];
  4995.     p[1] = p[2];
  4996.     p[2] = t;
  4997.     return;
  4998.  
  4999.     default:
  5000.     fatal("Internal Error in fix_byte_order, n = %d\n", 0);
  5001.     }
  5002. }
  5003.  
  5004. static void
  5005. fix_exec_header_byte_order(execP)
  5006.     struct exec *execP;
  5007. {
  5008.  
  5009. #if TARGET_MACHINE==TARGET_SUN4
  5010.     if (N_BADMAG(*execP)) {
  5011.     u_char c = * (u_char *) execP;
  5012.     execP->a_dynamic = c & 1;
  5013.     execP->a_toolversion = c >> 1;
  5014.     } else {
  5015.     u_char c = execP->a_toolversion | (execP->a_dynamic << 7);
  5016.     * (u_char *) execP = c;
  5017.     }
  5018. #else
  5019.     fix_byte_order(&execP->a_machtype, sizeof(execP->a_machtype));
  5020. #endif
  5021.     fix_byte_order(&execP->a_magic,    sizeof(execP->a_magic));
  5022.     fix_byte_order(&execP->a_text,     sizeof(execP->a_text));
  5023.     fix_byte_order(&execP->a_data,     sizeof(execP->a_data));
  5024.     fix_byte_order(&execP->a_bss,      sizeof(execP->a_bss));
  5025.     fix_byte_order(&execP->a_syms,     sizeof(execP->a_syms));
  5026.     fix_byte_order(&execP->a_entry,    sizeof(execP->a_entry));
  5027.     fix_byte_order(&execP->a_trsize,   sizeof(execP->a_trsize));
  5028.     fix_byte_order(&execP->a_drsize,   sizeof(execP->a_drsize));
  5029.     return;
  5030. }
  5031.  
  5032. static void
  5033. fix_symbol_byte_order(p, size)
  5034.     struct nlist *p;
  5035.     int size;
  5036. {
  5037.     int n;
  5038.  
  5039.     for (n = size / sizeof(*p); --n >= 0; ++p) {
  5040.     fix_byte_order(&p->n_un, sizeof(p->n_un));
  5041.     fix_byte_order(&p->n_desc, sizeof(p->n_desc));
  5042.     fix_byte_order(&p->n_value, sizeof(p->n_value));
  5043.     }
  5044.     return;
  5045. }
  5046.  
  5047. static void
  5048. target_to_host_reloc_byte_order(relocP, nrelocs)
  5049.     struct relocation_info *relocP;
  5050.     int nrelocs;
  5051. {
  5052.     u_char c[4];
  5053.  
  5054. #if TARGET_BYTE_ORDER==BIG_ENDIAN && HOST_BYTE_ORDER==LITTLE_ENDIAN
  5055.     for (; --nrelocs >= 0; ++relocP) {
  5056.     fix_byte_order(&relocP->r_address, sizeof(relocP->r_address));
  5057.     c[0] = ((u_char *) relocP)[4];
  5058.     c[1] = ((u_char *) relocP)[5];
  5059.     c[2] = ((u_char *) relocP)[6];
  5060.     c[3] = ((u_char *) relocP)[7];
  5061.     ((u_long *) relocP)[1] = 0;
  5062. #if TARGET_MACHINE==TARGET_SUN4
  5063.     relocP->r_index = (c[0] << 16) | (c[1] << 8) | c[2];
  5064.     relocP->r_type = (enum reloc_type) (c[3] & 0x1f);
  5065.     relocP->r_extern = (c[3] & 0x80) >> 7;
  5066.     fix_byte_order(&relocP->r_addend, sizeof(relocP->r_addend));
  5067. #else
  5068.     relocP->r_symbolnum = (c[0] << 16) | (c[1] << 8) | c[2];
  5069.     relocP->r_pcrel = (c[3] >> 7) & 1;
  5070.     relocP->r_length = (c[3] >> 5) & 3;
  5071.     relocP->r_extern = (c[3] >> 4) & 1;
  5072. #endif
  5073.     }
  5074. #endif
  5075.     return;
  5076. }
  5077.  
  5078. static void
  5079. host_to_target_reloc_byte_order(relocP, nrelocs)
  5080.     struct relocation_info *relocP;
  5081.     int nrelocs;
  5082. {
  5083.     u_char c[4];
  5084.  
  5085. #if TARGET_BYTE_ORDER==BIG_ENDIAN && HOST_BYTE_ORDER==LITTLE_ENDIAN
  5086.     for (; --nrelocs >= 0; ++relocP) {
  5087.     fix_byte_order(&relocP->r_address, sizeof(relocP->r_address));
  5088. #if TARGET_MACHINE==TARGET_SUN4
  5089.     c[0] = (relocP->r_index >> 16);
  5090.     c[1] = (relocP->r_index >> 8);
  5091.     c[2] = relocP->r_index;
  5092.     c[3] = (u_char) relocP->r_type | (relocP->r_extern << 7);
  5093.     fix_byte_order(&relocP->r_addend, sizeof(relocP->r_addend));
  5094. #else
  5095.     c[0] = (relocP->r_symbolnum >> 16);
  5096.     c[1] = (relocP->r_symbolnum >> 8);
  5097.     c[2] = relocP->r_symbolnum;
  5098.     c[3] = (relocP->r_pcrel << 7) |
  5099.         (relocP->r_length << 5) | (relocP->r_extern << 4);
  5100. #endif
  5101.     ((u_char *) relocP)[4] = c[0];
  5102.     ((u_char *) relocP)[5] = c[1];
  5103.     ((u_char *) relocP)[6] = c[2];
  5104.     ((u_char *) relocP)[7] = c[3];
  5105.     }
  5106. #endif
  5107.     return;
  5108. }
  5109.  
  5110. static void
  5111. fix_symbol_root_byte_order(p)
  5112.     struct symbol_root *p;
  5113. {
  5114.  
  5115.     fix_byte_order(&p->format, sizeof(p->format));
  5116.     fix_byte_order(&p->length, sizeof(p->length));
  5117.     fix_byte_order(&p->ldsymoff, sizeof(p->ldsymoff));
  5118.     fix_byte_order(&p->textrel, sizeof(p->textrel));
  5119.     fix_byte_order(&p->datarel, sizeof(p->datarel));
  5120.     fix_byte_order(&p->bssrel, sizeof(p->bssrel));
  5121.     fix_byte_order(&p->filename, sizeof(p->filename));
  5122.     fix_byte_order(&p->filedir, sizeof(p->filedir));
  5123.     fix_byte_order(&p->blockvector, sizeof(p->blockvector));
  5124.     fix_byte_order(&p->typevector, sizeof(p->typevector));
  5125.     fix_byte_order(&p->language, sizeof(p->language));
  5126.     fix_byte_order(&p->version, sizeof(p->version));
  5127.     fix_byte_order(&p->compilation, sizeof(p->compilation));
  5128.     fix_byte_order(&p->databeg, sizeof(p->databeg));
  5129.     fix_byte_order(&p->bssbeg, sizeof(p->bssbeg));
  5130.     fix_byte_order(&p->sourcevector, sizeof(p->sourcevector));
  5131.     return;
  5132. }
  5133.  
  5134. #endif
  5135.  
  5136.